简体   繁体   English

如何分隔所有公式并将其作为子类?

[英]How do I separate all the formulas and make it a subclass?

I want to separate all the formulas (a subclass for each currency) from the main method. 我想将所有公式(每种货币的子类)与main方法分开。 I want to make a subclass of formulas and call them on the main method. 我想使公式的子类,并呼吁他们的主要方法。 Any ideas on how to do it? 关于如何做的任何想法?

import java.util.*;

public class  CurrencyConverter
{
        public void print()
        {

            System.out.println("Pick an appropriate currency: \n\n 01 - Philippine Peso \n 02 - US Dollar \n 03 - Japan Yen \n 04 - UK Pounds \n 05 - Switzerland Franc \n 06 - Bahrain Dinar"); 
            System.out.println( " 07 - Saudi Arabia Rial \n 08 - Indonesia Rupiah \n 09 - UAE Dirham \n 10 - China Yuan \n 11 - Korea Won \n 12 - Europe Euro \n");
        }
        public static void main (String[]args)
        {   
            double peso_z = 1;
            double dollar_z = 45.17;
            double yen_z = 0.37;    
            double pound_z = 69.5;
            double franc_z = 47.64;
            double dinar_z = 120.05;
            double rial_z = 12.05;
            double baht_z = 1.33;
            double dirham_z = 12.30;
            double yuan_z = 7.28;
            double won_z = 0.04;
            double euro_z = 49.83;

            double rate = 0;

            CurrencyConverter w = new CurrencyConverter();
            w.print();

            System.out.print("Choose the input currency: ");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();

            String x = null;
            switch(choice) {
                case  1: x = "Philippine Peso: ";  
                            break;
                case  2: x = "US Dollar: ";  
                            break;
                case  3: x = "Japan Yen: ";  
                            break;
                case  4: x = "UK Pound: ";  
                            break;
                case  5: x = "Switzerland Franc: ";  
                            break; 
                case  6: x = "Bahrain Dinar: ";  
                            break;
                case  7: x = "Saudi Arabia Rial: ";  
                            break; 
                case  8: x = "Indonesia Baht: ";  
                            break;
                case  9: x = "UAE Dirham: ";  
                            break;
                case 10: x = "China Yuan: ";  
                            break;
                case 11: x = "Korea Won: ";  
                            break;
                case 12: x = "Europe Euro: ";  
                            break;
                default:    
                    System.out.println("404 not found XD");
                    return;
            }

                System.out.print("Choose the output currency: ");
                int output = sc.nextInt();

                System.out.printf("Now enter the input in " +x);
                double input = sc.nextDouble();

                if(choice == output)
                    System.out.println("Troller ._. pabobo");

                // ~ Peso ~
                if(choice == 1 && output == 2)
                {   
                    rate = input / dollar_z ;
                    System.out.print( "The Conversion of Peso to Dollar = " +rate);
                }
                else if(choice == 1 && output == 3)
                {   
                    rate = input / yen_z ;
                    System.out.print( "The Conversion of Peso to Yen = " +rate);
                }
                else if(choice == 1 && output == 4)
                {   
                    rate = input / pound_z ;
                    System.out.print( "The Conversion of Peso to Pound = " +rate);
                }
                else if(choice == 1 && output == 5)
                {   
                    rate = input / franc_z ;
                    System.out.print( "The Conversion of Peso to Franc = " +rate);
                }
                else if(choice == 1 && output == 6)
                {   
                    rate = input / dinar_z ;
                    System.out.print( "The Conversion of Peso to Dinar = " +rate);
                }
                else if(choice == 1 && output == 7)
                {   
                    rate = input / rial_z ;
                    System.out.print( "The Conversion of Peso to Rial = " +rate);
                }
                else if(choice == 1 && output == 8)
                {   
                    rate = input / baht_z ;
                    System.out.print( "The Conversion of Peso to Baht = " +rate);
                }
                else if(choice == 1 && output == 9)
                {   
                    rate = input / dirham_z ;
                    System.out.print( "The Conversion of Peso to Dirham = " +rate);
                }
                else if(choice == 1 && output == 10)
                {   
                    rate = input / yuan_z ;
                    System.out.print( "The Conversion of Peso to Yuan = " +rate);
                }
                else if(choice == 1 && output == 11)
                {   
                    rate = input / won_z ;
                    System.out.print( "The Conversion of Peso to Won = " +rate);
                }
                else if(choice == 1 && output == 12)
                {   
                    rate = input / euro_z ;
                    System.out.print( "The Conversion of Peso to Euro = " +rate);
                }

                //Dollar
                if(choice == 2 && output == 1)
                {   
                    rate = input * dollar_z ;
                    System.out.print( "The Conversion of Dollar to Peso = " +rate);
                }
                else if(choice == 2 && output == 3)
                {   
                    rate = (input * dollar_z) / yen_z ;
                    System.out.print( "The Conversion of Dollar to Yen = " +rate);
                }
                else if(choice == 2 && output == 4)
                {   
                    rate = (input * dollar_z) / pound_z ;
                    System.out.print( "The Conversion of Dollar to Pound = " +rate);
                }
                else if(choice == 2 && output == 5)
                {   
                    rate = (input * dollar_z) / franc_z ;
                    System.out.print( "The Conversion of Dollar to Franc = " +rate);
                }
                else if(choice == 2 && output == 6)
                {   
                    rate = (input * dollar_z) / dinar_z ;
                    System.out.print( "The Conversion of Dollar to Dinar = " +rate);
                }
                else if(choice == 2 && output == 7)
                {   
                    rate = (input * dollar_z) / rial_z ;
                    System.out.print( "The Conversion of Dollar to Rial = " +rate);
                }
                else if(choice == 2 && output == 8)
                {   
                    rate = (input * dollar_z) / baht_z ;
                    System.out.print( "The Conversion of Dollar to Baht = " +rate);
                }
                else if(choice == 2 && output == 9)
                {   
                    rate = (input * dollar_z) / dirham_z ;
                    System.out.print( "The Conversion of Dollar to Dirham = " +rate);
                }
                else if(choice == 2 && output == 10)
                {   
                    rate = (input * dollar_z) / yuan_z ;
                    System.out.print( "The Conversion of Dollar to Yuan = " +rate);
                }
                else if(choice == 2 && output == 11)
                {   
                    rate = (input * dollar_z) / won_z ;
                    System.out.print( "The Conversion of Dollar to Won = " +rate);
                }
                else if(choice == 2 && output == 12)
                {   
                    rate = (input * dollar_z) / euro_z ;
                    System.out.print( "The Conversion of Dollar to Euro = " +rate);
                }

                // ~ Yen ~
                if(choice == 3 && output == 1)
                {   
                    rate = (input * yen_z);
                    System.out.print( "The Conversion of Yen to Peso = " +rate);
                }
                else if(choice == 3 && output == 2)
                {   
                    rate = (input * yen_z) / dollar_z ;
                    System.out.print( "The Conversion of Yen to Dollar = " +rate);
                }
                else if(choice == 3 && output == 4)
                {   
                    rate = (input * yen_z) / pound_z ;
                    System.out.print( "The Conversion of Yen to Pound = " +rate);
                }
                else if(choice == 3 && output == 5)
                {   
                    rate = (input * yen_z) / franc_z ;
                    System.out.print( "The Conversion of Yen to Franc = " +rate);
                }
                else if(choice == 3 && output == 6)
                {   
                    rate = (input * yen_z) / dinar_z ;
                    System.out.print( "The Conversion of Yen to Dinar = " +rate);
                }
                else if(choice == 3 && output == 7)
                {   
                    rate = (input * yen_z) / rial_z ;
                    System.out.print( "The Conversion of Yen to Rial = " +rate);
                }
                else if(choice == 3 && output == 8)
                {   
                    rate = (input * yen_z) / baht_z ;
                    System.out.print( "The Conversion of Yen to Baht = " +rate);
                }
                else if(choice == 3 && output == 9)
                {   
                    rate = (input * yen_z) / dirham_z ;
                    System.out.print( "The Conversion of Yen to Dirham = " +rate);
                }
                else if(choice == 3 && output == 10)
                {   
                    rate = (input * yen_z) / yuan_z ;
                    System.out.print( "The Conversion of Yen to Yuan = " +rate);
                }
                else if(choice == 3 && output == 11)
                {   
                    rate = (input * yen_z) / won_z ;
                    System.out.print( "The Conversion of Yen to Won = " +rate);
                }
                else if(choice == 3 && output == 12)
                {   
                    rate = (input * yen_z) / euro_z ;
                    System.out.print( "The Conversion of Yen to Euro = " +rate);
                }

                // ~ Pound ~
                if(choice == 4 && output == 1)
                {   
                    rate = input * pound_z ;
                    System.out.print( "The Conversion of Pound to Peso = " +rate);
                }
                else if(choice == 4 && output == 2)
                {   
                    rate = (input * pound_z) / dollar_z ;
                    System.out.print( "The Conversion of Pound to Dollar = " +rate);
                }
                else if(choice == 4 && output == 3)
                {   
                    rate = (input * pound_z) / yen_z ;
                    System.out.print( "The Conversion of Pound to Yen = " +rate);
                }
                else if(choice == 4 && output == 5)
                {   
                    rate = (input * pound_z) / franc_z ;
                    System.out.print( "The Conversion of Pound to Franc = " +rate);
                }
                else if(choice == 4 && output == 6)
                {   
                    rate = (input * pound_z) / dinar_z ;
                    System.out.print( "The Conversion of Pound to Dinar = " +rate);
                }
                else if(choice == 4 && output == 7)
                {   
                    rate = (input * pound_z) / rial_z ;
                    System.out.print( "The Conversion of Pound to Rial = " +rate);
                }
                else if(choice == 4 && output == 8)
                {   
                    rate = (input * pound_z) / baht_z ;
                    System.out.print( "The Conversion of Pound to Baht = " +rate);
                }
                else if(choice == 4 && output == 9)
                {   
                    rate = (input * pound_z) / dirham_z ;
                    System.out.print( "The Conversion of Pound to Dirham = " +rate);
                }    
                else if(choice == 4 && output == 10)
                {   
                    rate = (input * pound_z) / yuan_z ;
                    System.out.print( "The Conversion of Pound to Yuan = " +rate);
                }
                else if(choice == 4 && output == 11)
                {   
                    rate = (input * pound_z) / won_z ;
                    System.out.print( "The Conversion of Pound to Won = " +rate);
                }
                else if(choice == 4 && output == 12)
                {   
                    rate = (input * pound_z) / euro_z ;
                    System.out.print( "The Conversion of Pound to Euro = " +rate);
                }

                // ~ Franc ~
                if(choice == 5 && output == 1)
                {   
                    rate = input * franc_z ;
                    System.out.print( "The Conversion of Franc to Peso = " +rate);
                }
                else if(choice == 5 && output == 2)
                {   
                    rate = (input * franc_z) / dollar_z ;
                    System.out.print( "The Conversion of Franc to Dollar = " +rate);
                }
                else if(choice == 5 && output == 3)
                {   
                    rate = (input * franc_z) / yen_z ;
                    System.out.print( "The Conversion of Franc to Yen = " +rate);
                }
                else if(choice == 5 && output == 4)
                {   
                    rate = (input * franc_z) / pound_z ;
                    System.out.print( "The Conversion of Franc to Pound = " +rate);
                }
                else if(choice == 5 && output == 6)
                {   
                    rate = (input * franc_z) / dinar_z ;
                    System.out.print( "The Conversion of Franc to Dinar = " +rate);
                }
                else if(choice == 5 && output == 7)
                {   
                    rate = (input * franc_z) / rial_z ;
                    System.out.print( "The Conversion of Franc to Rial = " +rate);
                }
                else if(choice == 5 && output == 8)
                {   
                    rate = (input * franc_z) / baht_z ;
                    System.out.print( "The Conversion of Franc to Baht = " +rate);
                }
                else if(choice == 5 && output == 9)
                {   
                    rate = (input * franc_z) / dirham_z ;
                    System.out.print( "The Conversion of Franc to Dirham = " +rate);
                }
                else if(choice == 5 && output == 10)
                {   
                    rate = (input * franc_z) / yuan_z ;
                    System.out.print( "The Conversion of Franc to Yuan = " +rate);
                }
                else if(choice == 5 && output == 11)
                {   
                    rate = (input * franc_z) / won_z ;
                    System.out.print( "The Conversion of Franc to Won = " +rate);
                }
                else if(choice == 5 && output == 12)
                {   
                    rate = (input * franc_z) / euro_z ;
                    System.out.print( "The Conversion of Franc to Euro = " +rate);
                }

                // ~ Dinar ~
                if(choice == 6 && output == 1)
                {   
                    rate = input * dinar_z;
                    System.out.print( "The Conversion of Dinar to Peso = " +rate);
                }
                else if(choice == 6 && output == 2)
                {   
                    rate = (input * dinar_z) / dollar_z ;
                    System.out.print( "The Conversion of Dinar to Dollar = " +rate);
                }
                else if(choice == 6 && output == 3)
                {   
                    rate = (input * dinar_z) / yen_z ;
                    System.out.print( "The Conversion of Dinar to Yen = " +rate);
                }
                else if(choice == 6 && output == 4)
                {   
                    rate = (input * dinar_z) / pound_z ;
                    System.out.print( "The Conversion of Dinar to Pound = " +rate);
                }
                else if(choice == 6 && output == 5)
                {   
                    rate = (input * dinar_z) / franc_z ;
                    System.out.print( "The Conversion of Dinar to franc = " +rate);
                }
                else if(choice == 6 && output == 7)
                {   
                    rate = (input * dinar_z) / rial_z ;
                    System.out.print( "The Conversion of Dinar to Rial = " +rate);
                }
                else if(choice == 6 && output == 8)
                {   
                    rate = (input * dinar_z) / baht_z ;
                    System.out.print( "The Conversion of Dinar to Baht = " +rate);
                }
                else if(choice == 6 && output == 9)
                {   
                    rate = (input * dinar_z) / dirham_z ;
                    System.out.print( "The Conversion of Dinar to Dirham = " +rate);
                }
                else if(choice == 6 && output == 10)
                {   
                    rate = (input * dinar_z) / yuan_z ;
                    System.out.print( "The Conversion of Dinar to Yuan = " +rate);
                }
                else if(choice == 6 && output == 11)
                {   
                    rate = (input * dinar_z) / won_z ;
                    System.out.print( "The Conversion of Dinar to Won = " +rate);
                }
                else if(choice == 6 && output == 12)
                {   
                    rate = (input * dinar_z) / euro_z ;
                    System.out.print( "The Conversion of Dinar to Euro = " +rate);
                }

                // ~ Rial ~
                if(choice == 7 && output == 1)
                {   
                    rate = input * rial_z ;
                    System.out.print( "The Conversion of Rial to Peso = " +rate);
                }
                else if(choice == 7 && output == 2)
                {   
                    rate = (input * rial_z) / dollar_z ;
                    System.out.print( "The Conversion of Rial to Dollar = " +rate);
                }
                else if(choice == 7 && output == 3)
                {   
                    rate = (input * rial_z) / yen_z ;
                    System.out.print( "The Conversion of Rial to Yen = " +rate);
                }
                else if(choice == 7 && output == 4)
                {   
                    rate = (input * rial_z) / pound_z ;
                    System.out.print( "The Conversion of Rial to Pound = " +rate);
                }
                else if(choice == 7 && output == 5)
                {   
                    rate = (input * rial_z) / franc_z ;
                    System.out.print( "The Conversion of Rial to Franc = " +rate);
                }
                else if(choice == 7 && output == 6)
                {   
                    rate = (input * rial_z) / dinar_z ;
                    System.out.print( "The Conversion of Rial to Dinar = " +rate);
                }
                else if(choice == 7 && output == 8)
                {   
                    rate = (input * rial_z) / baht_z ;
                    System.out.print( "The Conversion of Rial to Baht = " +rate);
                }
                else if(choice == 7 && output == 9)
                {   
                    rate = (input * rial_z) / dirham_z ;
                    System.out.print( "The Conversion of Rial to Dirham = " +rate);
                }
                else if(choice == 7 && output == 10)
                {   
                    rate = (input * rial_z) / yuan_z ;
                    System.out.print( "The Conversion of Rial to Yuan = " +rate);
                }
                else if(choice == 7 && output == 11)
                {   
                    rate = (input * rial_z) / won_z ;
                    System.out.print( "The Conversion of Rial to Won = " +rate);
                }
                else if(choice == 7 && output == 2)
                {   
                    rate = (input * rial_z) / euro_z ;
                    System.out.print( "The Conversion of Rial to Euro = " +rate);
                }

                // ~ Baht ~
                if(choice == 8 && output == 1)
                {   
                    rate = input * baht_z;
                    System.out.print( "The Conversion of Baht to Peso = " +rate);
                }
                else if(choice == 8 && output == 2)
                {   
                    rate = (input * baht_z) / dollar_z ;
                    System.out.print( "The Conversion of Baht to Dollar = " +rate);
                }
                else if(choice == 8 && output == 3)
                {   
                    rate = (input * baht_z) / yen_z ;
                    System.out.print( "The Conversion of Baht to Yen = " +rate);
                }
                else if(choice == 8 && output == 4)
                {   
                    rate = (input * baht_z) / pound_z ;
                    System.out.print( "The Conversion of Baht to Pound = " +rate);
                }
                else if(choice == 8 && output == 5)
                {   
                    rate = (input * baht_z) / franc_z ;
                    System.out.print( "The Conversion of Baht to Franc = " +rate);
                }
                else if(choice == 8 && output == 6)
                {   
                    rate = (input * baht_z) / dinar_z ;
                    System.out.print( "The Conversion of Baht to Dinar = " +rate);
                }
                else if(choice == 8 && output == 7)
                {   
                    rate = (input * baht_z) / rial_z ;
                    System.out.print( "The Conversion of Baht to Rial = " +rate);
                }
                else if(choice == 8 && output == 9)
                {   
                    rate = (input * baht_z) / dirham_z ;
                    System.out.print( "The Conversion of Baht to Dirham = " +rate);
                }
                else if(choice == 8 && output == 10)
                {   
                    rate = (input * baht_z) / yuan_z ;
                    System.out.print( "The Conversion of Baht to Yuan = " +rate);
                }
                else if(choice == 8 && output == 11)
                {   
                    rate = (input * baht_z) / won_z ;
                    System.out.print( "The Conversion of Baht to Won = " +rate);
                }
                else if(choice == 8 && output == 12)
                {   
                    rate = (input * baht_z) / euro_z ;
                    System.out.print( "The Conversion of Baht to Euro = " +rate);
                }

                // ~ Dirham ~
                if(choice == 9 && output == 1)
                {   
                    rate = input * dirham_z;
                    System.out.print( "The Conversion of Dirham to Peso = " +rate);
                }
                else if(choice == 9 && output == 2)
                {   
                    rate = (input * dirham_z) / dollar_z ;
                    System.out.print( "The Conversion of Dirham to Dollar = " +rate);
                }
                else if(choice == 9 && output == 3)
                {   
                    rate = (input * dirham_z) / yen_z ;
                    System.out.print( "The Conversion of Dirham to Yen = " +rate);
                }
                else if(choice == 9 && output == 4)
                {   
                    rate = (input * dirham_z) / pound_z ;
                    System.out.print( "The Conversion of Dirham to Pound = " +rate);
                }
                else if(choice == 9 && output == 5)
                {   
                    rate = (input * dirham_z) / franc_z ;
                    System.out.print( "The Conversion of Dirham to Franc = " +rate);
                }
                else if(choice == 9 && output == 6)
                {   
                    rate = (input * dirham_z) / dinar_z ;
                    System.out.print( "The Conversion of Dirham to Dinar = " +rate);
                }
                else if(choice == 9 && output == 7)
                {   
                    rate = (input * dirham_z) / rial_z ;
                    System.out.print( "The Conversion of Dirham to Rial = " +rate);
                }
                else if(choice == 9 && output == 8)
                {   
                    rate = (input * dirham_z) / baht_z ;
                    System.out.print( "The Conversion of Dirham to Baht = " +rate);
                }
                else if(choice == 9 && output == 10)
                {   
                    rate = (input * dirham_z) / yuan_z ;
                    System.out.print( "The Conversion of Dirham to Yuan = " +rate);
                }
                else if(choice == 9 && output == 11)
                {   
                    rate = (input * dirham_z) / won_z ;
                    System.out.print( "The Conversion of Dirham to Won = " +rate);
                }
                else if(choice == 9 && output == 12)
                {   
                    rate = (input * dirham_z) / euro_z ;
                    System.out.print( "The Conversion of Dirham to Euro = " +rate);
                }

                // ~ Yuan ~
                if(choice == 10 && output == 1)
                {   
                    rate = input * yuan_z;
                    System.out.print( "The Conversion of Yuan to Peso = " +rate);
                }
                else if(choice == 10 && output == 2)
                {   
                    rate = (input * yuan_z) / dollar_z ;
                    System.out.print( "The Conversion of Yuan to Dollar = " +rate);
                }
                else if(choice == 10 && output == 3)
                {   
                    rate = (input * yuan_z) / yen_z ;
                    System.out.print( "The Conversion of Yuan to Yen = " +rate);
                }
                else if(choice == 10 && output == 4)
                {   
                    rate = (input * yuan_z) / pound_z ;
                    System.out.print( "The Conversion of Yuan to Pound = " +rate);
                }
                else if(choice == 10 && output == 5)
                {   
                    rate = (input * yuan_z) / franc_z ;
                    System.out.print( "The Conversion of Yuan to Franc = " +rate);
                }
                else if(choice == 10 && output == 6)
                {   
                    rate = (input * yuan_z) / dinar_z ;
                    System.out.print( "The Conversion of Yuan to Dinar = " +rate);
                }
                else if(choice == 10 && output == 7)
                {   
                    rate = (input * yuan_z) / rial_z ;
                    System.out.print( "The Conversion of Yuan to Rial = " +rate);
                }
                else if(choice == 10 && output == 8)
                {   
                    rate = (input * yuan_z) / baht_z ;
                    System.out.print( "The Conversion of Yuan to Baht = " +rate);
                }
                else if(choice == 10 && output == 9)
                {   
                    rate = (input * yuan_z) / dirham_z ;
                    System.out.print( "The Conversion of Yuan to Dirham = " +rate);
                }
                else if(choice == 10 && output == 11)
                {   
                    rate = (input * yuan_z) / won_z ;
                    System.out.print( "The Conversion of Yuan to Won = " +rate);
                }
                else if(choice == 10 && output == 12)
                {   
                    rate = (input * yuan_z) / euro_z ;
                    System.out.print( "The Conversion of Yuan to Euro = " +rate);
                }

                // ~ Won ~
                if(choice == 11 && output == 1)
                {   
                    rate = input * won_z;
                    System.out.print( "The Conversion of Won to Peso = " +rate);
                }
                else if(choice == 11 && output == 2)
                {   
                    rate = (input * won_z) / dollar_z ;
                    System.out.print( "The Conversion of Won to Dollar = " +rate);
                }
                else if(choice == 11 && output == 3)
                {   
                    rate = (input * won_z) / yen_z ;
                    System.out.print( "The Conversion of Won to Yen = " +rate);
                }
                else if(choice == 11 && output == 4)
                {   
                    rate = (input * won_z) / pound_z ;
                    System.out.print( "The Conversion of Won to Pound = " +rate);
                }
                else if(choice == 11 && output == 5)
                {   
                    rate = (input * won_z) / franc_z ;
                    System.out.print( "The Conversion of Won to Franc = " +rate);
                }
                else if(choice == 11 && output == 6)
                {   
                    rate = (input * won_z) / dinar_z ;
                    System.out.print( "The Conversion of Won to Dinar = " +rate);
                }
                else if(choice == 11 && output == 7)
                {   
                    rate = (input * won_z) / rial_z ;
                    System.out.print( "The Conversion of Won to Rial = " +rate);
                }
                else if(choice == 11 && output == 8)
                {   
                    rate = (input * won_z) / baht_z ;
                    System.out.print( "The Conversion of Won to Baht = " +rate);
                }
                else if(choice == 11 && output == 9)
                {   
                    rate = (input * won_z) / dirham_z ;
                    System.out.print( "The Conversion of Won to Dirham = " +rate);
                }
                else if(choice == 11 && output == 10)
                {   
                    rate = (input * won_z) / yuan_z ;
                    System.out.print( "The Conversion of Won to Yuan = " +rate);
                }
                else if(choice == 11 && output == 12)
                {   
                    rate = (input * won_z) / euro_z ;
                    System.out.print( "The Conversion of Won to Euro = " +rate);
                }

                // ~ Euro ~
                if(choice == 12 && output == 1)
                {   
                    rate = input * euro_z;
                    System.out.print( "The Conversion of Euro to Peso = " +rate);
                }
                else if(choice == 12 && output == 2)
                {   
                    rate = (input * euro_z) / dollar_z ;
                    System.out.print( "The Conversion of Euro to Dollar = " +rate);
                }
                else if(choice == 12 && output == 3)
                {   
                    rate = (input * euro_z) / yen_z ;
                    System.out.print( "The Conversion of Euro to Yen = " +rate);
                }
                else if(choice == 12 && output == 4)
                {   
                    rate = (input * euro_z) / pound_z ;
                    System.out.print( "The Conversion of Euro to Pound = " +rate);
                }
                else if(choice == 12 && output == 5)
                {   
                    rate = (input * euro_z) / franc_z ;
                    System.out.print( "The Conversion of Euro to Franc = " +rate);
                }
                else if(choice == 12 && output == 6)
                {   
                    rate = (input * euro_z) / dinar_z ;
                    System.out.print( "The Conversion of Euro to Dinar = " +rate);
                }
                else if(choice == 12 && output == 7)
                {   
                    rate = (input * euro_z) / rial_z ;
                    System.out.print( "The Conversion of Euro to Rial = " +rate);
                }
                else if(choice == 12 && output == 8)
                {   
                    rate = (input * euro_z) / baht_z ;
                    System.out.print( "The Conversion of Euro to Baht = " +rate);
                }
                else if(choice == 12 && output == 9)
                {   
                    rate = (input * euro_z) / dirham_z ;
                    System.out.print( "The Conversion of Euro to Dirham = " +rate);
                }
                else if(choice == 12 && output == 10)
                {   
                    rate = (input * euro_z) / yuan_z ;
                    System.out.print( "The Conversion of Euro to Yuan = " +rate);
                }
                else if(choice == 12 && output == 11)
                {   
                    rate = (input * euro_z) / won_z ;
                    System.out.print( "The Conversion of Euro to Won = " +rate);
                }   
        }
}

Use an enum , and then plug the appropriate enum instances using methods instead of checking state. 使用一个enum ,然后使用方法而不是检查状态来插入适当的enum实例。 This code works identically to yours including output. 此代码与您的代码(包括输出)完全相同。

Here's some code. 这是一些代码。 The enum is at the top: 枚举位于顶部:

import java.util.*;

public class CurrencyConverter {
  public static enum Currency {
    Peso (1, "Phillipine Peso"),
    Dollar (45.17, "US Dollar"),
    Yen (0.37, "Japan Yen"),
    Pound (69.5, "UK Pound"),
    Franc (47.64, "Switzerland Franc"),
    Dinar (120.05, "Bahrain Dinar"),
    Rial (12.05, "Saudi Arabia Rial"),
    Baht (1.33, "Indonesia Baht"),
    Dirham (12.30, "UAE Dirham"),
    Yuan (7.28, "China Yuan"),
    Won (0.04, "Korea Won"),
    Euro (49.83, "Europe Euro");

    private final double rate;
    private final String fullName;

    private Currency(double rate, String fullName) {
      this.rate = rate;
      this.fullName = fullName;
    }

    public double getRate() {
      return rate;
    }

    public String getFullName() {
      return fullName;
    }
  }

  public void print() {
    System.out.println("Pick an appropriate currency: \n\n");
    for(Currency currency : Currency.values()) {
      System.out.format(" %2d - %s %n", currency.ordinal() + 1, currency.fullName);
    }
  }

  public static void main(String[] args) {

    double rate = 0;

    CurrencyConverter w = new CurrencyConverter();
    w.print();

    System.out.print("Choose the input currency: ");
    Scanner sc = new Scanner(System.in);
    int choice = sc.nextInt();

    if(choice < 1 || choice > 12) {
      System.out.println("404 not found XD");

      sc.close();

      return;
    }

    System.out.print("Choose the output currency: ");
    int output = sc.nextInt();

    Currency first = Currency.values()[choice-1];
    Currency second = Currency.values()[output-1];

    System.out.printf("Now enter the input in " + first.getFullName() + ": ");
    double input = sc.nextDouble();

    if (choice == output)
      System.out.println("Troller ._. pabobo");

    rate = (input * first.getRate()) / second.getRate();
    System.out.print("The conversion of " + first.toString() + " to "
        + second.toString() + " = " + rate);

    sc.close();
  }
}

If you use an enum things get very much simpler. 如果使用enum事情会变得非常简单。

Something like this should do for a starting point. 这样的事情应该为起点做。 I've only guessed at the math but this should show you much more clearly how easy things can get when you use the right structure. 我只是在数学上猜测过,但这应该可以更清楚地向您显示使用正确的结构可以轻松实现。

public class CurrencyConverter {

    public void print() {

        System.out.println("Pick an appropriate currency:");
        for (Currency c : Currency.values()) {
            System.out.println(c.ordinal() + " - " + c.description);
        }
    }

    enum Currency {

        Peso(1, "Philippine Peso"),
        Dollar(45.17, "US Dollar"),
        Yen(0.37, "Japan Yen"),
        Pound(69.5, "UK Pounds"),
        Franc(47.64, "Switzerland Franc"),
        Dinar(120.05, "Bahrain Dinar"),
        Rial(12.05, "Saudi Arabia Rial"),
        Baht(1.33, "Indonesia Rupiah"),
        Dirham(12.30, "UAE Dirham"),
        Yuan(7.28, "China Yuan"),
        Won(0.04, "Korea Won"),
        Euro(49.83, "Europe Eur");
        private final double conversion;
        private final String description;

        private Currency(double conversion, String description) {
            this.conversion = conversion;
            this.description = description;
        }
    }

    public static void main(String[] args) {
        double rate = 0;

        CurrencyConverter w = new CurrencyConverter();
        w.print();

        System.out.print("Choose the input currency: ");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        // Do some boundary checks here!
        Currency from = Currency.values()[choice];

        System.out.print("Choose the output currency: ");
        int output = sc.nextInt();
        // Do some boundary checks here!
        Currency to = Currency.values()[output];

        System.out.printf("Now enter the input in " + from.description + ": ");
        double input = sc.nextDouble();

        if (from == to) {
            System.out.println("Troller ._. pabobo");
        }
        // Not sure of the maths but somethiong like:
        rate = to.conversion / from.conversion;
        System.out.print("The Conversion of " + from.description + " to " + to.description + " = " + rate);
    }
}

Yes that is really all you need - except for getting the math correct. 是的,这确实是你所需要 -除了获得数学正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM