2014-10-20 45 views
-1

这是我的基本货币转换器,用于介绍我的Java类。我应该可以使用静态汇率在日元,美元,英镑和欧元之间进行转换。它有效,但我很想知道我是否以最有效的方式做到了。这似乎很长,看起来像一个巨大的混乱。只是想要一些反馈。基本货币转换器 - 寻找反馈

import java.util.Scanner; 

public class currency 
{ 

    public currency() 
    { 
     char us_dollar_sym = 36; 
     char pound_sym = 163; 
     char yen_sym = 165; 
     char euro_sym = 8364; 

     String us_dollar = "Dollars"; 
     String pound = "Pounds"; 
     String yen = "Yen"; 
     String euro = "Euros"; 
     double rate = 0; 


     // Interface 
     System.out.println("Welcome to the Currency Converter Program \n"); 
     System.out.println("Use the following codes to input your currency choices: \n 1 - US dollars \n 2 - Euros \n 3 - British Pounds \n 4 - Japanese Yen \n"); 

     // 
     System.out.println("Please choose the input currency:"); 
     Scanner in = new Scanner(System.in); 
     int choice = in.nextInt(); 

     String inType = null; 
     switch(choice) { 
     case 1: inType = "US Dollars >> " + us_dollar_sym; break; 
     case 2: inType = "Euros >> " + euro_sym; break; 
     case 3: inType = "British Pounds >> " + pound_sym; break; 
     case 4: inType = "Japanese Yen >> " + yen_sym; break; 
     default: 
     System.out.println("Please restart the program & enter a number from the list."); 
     return; 
     } 

     System.out.println("Please choose the output currency"); 
     int output = in.nextInt(); 

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

     if (choice == output) 
     System.out.println("Same currency no need to convert"); 

     if (choice == 1 && output == 2) 
     { 
      double dollar_euro_rate = 0.78391; 
      rate = input * dollar_euro_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + dollar_euro_rate + " Dollars to %s = %.2f\n", (char)us_dollar_sym, euro, rate); 
     } 
     else if (choice == 1 && output == 3){ 
      double dollar_pound_rate = 0.621484; 
      rate = input * dollar_pound_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + dollar_pound_rate + " Dollars to %s = %.2f\n", (char)us_dollar_sym, pound, rate); 
     } 
     else if (choice == 1 && output == 4){ 
      double dollar_yen_rate = 107.174; 
      rate = input * dollar_yen_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + dollar_yen_rate + " Dollars to %s = %.2f\n", (char)us_dollar_sym, yen, rate); 
     } 
     if (choice == 2 && output == 1) 
     { 
      double euro_dollar_rate = 1.27579; 
      rate = input * euro_dollar_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + euro_dollar_rate + " Euros to %s = %.2f\n", (char)euro_sym, us_dollar, rate); 
     } 
     else if (choice == 2 && output == 3) 
     { 
      double euro_pound_rate = 0.792648; 
      rate = input * euro_pound_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + euro_pound_rate + " Euros to %s = %.2f\n", (char)euro_sym, pound, rate); 
     } 
     else if (choice == 2 && output == 4) 
     { 
      double euro_yen_rate = 136.708; 
      rate = input * euro_yen_rate; 
      System.out.printf("%s" + input + " at a conversion rate of " + euro_yen_rate + " Euros to %s = %.2f\n", (char)euro_sym, yen, rate); 
     } 
     if (choice == 3 && output == 1) 
     { 
      double pound_dollar_rate = 1.60972; 
      System.out.printf("%s" + input + " at a conversion rate of " + pound_dollar_rate + " Pounds to %s = %.2f\n", (char)pound_sym, us_dollar, rate); 
     } 
     else if (choice == 3 && output == 2) 
     { 
      double pound_euro_rate = 1.26161; 
      System.out.printf("%s" + input + " at a conversion rate of " + pound_euro_rate + " Pounds to %s = %.2f\n", (char)pound_sym, euro, rate); 
     } 
     else if (choice == 3 && output == 4) 
     { 
      double pound_yen_rate = 172.511; 
      System.out.printf("%s" + input + " at a conversion rate of " + pound_yen_rate + " Pounds to %s = %.2f\n", (char)pound_sym, yen, rate); 
     } 
     if (choice == 4 && output == 1) 
     { 
      double yen_dollar_rate = 0.00932574; 
      System.out.printf("%s" + input + " at a conversion rate of " + yen_dollar_rate + " Yen to %s = %.2f\n", (char)yen_sym, us_dollar, rate); 
     } 
     else if (choice == 4 && output == 2) 
     { 
      double yen_euro_rate = 0.00730615; 
      System.out.printf("%s" + input + " at a conversion rate of " + yen_euro_rate + " Yen to %s = %.2f\n", (char)yen_sym, euro, rate); 
     } 
     else if (choice == 4 && output == 3) 
     { 
      double yen_pound_rate = 0.00579135; 
      System.out.printf("%s" + input + " at a conversion rate of " + yen_pound_rate + " Yen to %s = %.2f\n", (char)yen_sym, pound, rate); 
     } 
     System.out.println("Thank you for using the currency converter"); 
    } 
} 
+4

我认为这个程序工作(如你写的有)?如果是,那么最好将它发布在[codereview.se]中。 – 2014-10-20 02:28:25

回答

0

我总是小心使用整数输入。如果有人输入字符,程序可能会崩溃,因为会发生异常。接受号码的字符可能是更安全的选择。

当你在同勾选“选择”和“输出” if语句它需要更多的资源。嵌套if语句可能会提高效率。这将有if语句供if语句在内部输出。

0

既然你是指你的货币如int,我会使用由M矩阵正用于存储汇率。 n是第一个货币,第二个是m。用这两个int你可以回溯正确的汇率。

矩阵中的对角线将为1(因为USD >> USD = 1)。

最后,编写一个函数来计算汇率,并返回相应的文本(你可以使用的HashMap的是,随着INT作为键和名称(字符串)作为值)

exchange_rate = currency[1][2]; 


HashMap hm = new HashMap(); 

hm.put(1, new string("USD"); 

等等

0

你的利率作为一个标准值的倍数的所有快递,例如使用美元作为标准值。那么英镑的兑换价值为1.60,美元为1.0,欧元为1.29。然后,转换运算将是:

从值*从转换* 1 /要转换

例如从1 GBP欧将是:

1 * 1.60 *(1/1.29)= 1.24

如果您储存所有的利率在一个HashMap,那么你可以避开switch语句完全。

0

为什么你使用这种方式来转换货币。你有JSR 354金钱和货币API这里有一些例子,你可以使用,它很容易使用,快速:

MonetaryAmount monetaryAmount = Money.of(100.20, usd); 
CurrencyUnit currency = monetaryAmount.getCurrency(); 
NumberValue numberValue = monetaryAmount.getNumber(); 
  
int value= numberValue.intValue();