如何分离所有公式并使其成为子类?
How do I separate all the formulas and make it a subclass?
我想将所有公式(每种货币的子类)与主要方法分开。我想制作一个公式的子类并在 main 方法上调用它们。
有什么想法吗?
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);
}
}
}
使用 enum
,然后使用方法而不是检查状态插入适当的枚举实例。此代码与您的代码工作方式相同,包括输出。
这是一些代码。枚举在顶部:
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();
}
}
如果您使用 enum
,事情会 非常 简单得多。
像这样的事情应该作为一个起点。我只是对数学进行了猜测,但这应该会更清楚地向您展示当您使用正确的结构时事情会变得多么容易。
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);
}
}
是的,这确实是您需要的所有 - 除了数学正确。
我想将所有公式(每种货币的子类)与主要方法分开。我想制作一个公式的子类并在 main 方法上调用它们。 有什么想法吗?
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);
}
}
}
使用 enum
,然后使用方法而不是检查状态插入适当的枚举实例。此代码与您的代码工作方式相同,包括输出。
这是一些代码。枚举在顶部:
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();
}
}
如果您使用 enum
,事情会 非常 简单得多。
像这样的事情应该作为一个起点。我只是对数学进行了猜测,但这应该会更清楚地向您展示当您使用正确的结构时事情会变得多么容易。
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);
}
}
是的,这确实是您需要的所有 - 除了数学正确。