Java 基本货币转换器 - 寻求反馈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26457601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic Currency Converter - Looking for feedback
提问by 2redgoose
This is my basic currency converter for my intro to java class. I'm supposed to be able to convert between yen, dollars, pounds and euros using static rates. It works, but I was curious to know if I did it in the most efficient way possible. It seems quite long and looks like a huge mess. Just wanting some feedback.
这是我介绍 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");
}
}
回答by DasScooter
I am always cautious of using integer input. If someone types a char, your program would likely crash as an exception would occur. Accepting a char of the number might be a safer alternative.
我总是谨慎使用整数输入。如果有人输入字符,您的程序可能会因为异常发生而崩溃。接受数字的字符可能是一个更安全的选择。
When you check the "choice" and "output" in the same if statement it requires more resources. Nesting the if statements might improve efficiency. This would be having the if statement for choice with the if statements for output within them.
当您在同一个 if 语句中检查“选择”和“输出”时,它需要更多资源。嵌套 if 语句可能会提高效率。这将有用于选择的 if 语句和用于输出的 if 语句。
回答by Luc Berthiaume
Since you refer to your currency as int, I would use an n by m matrix for storing the exchange rate. n would be the first currency and m the second. With both int you can retrace the correct exchange rate.
由于您将货币称为 int,因此我将使用 n × m 矩阵来存储汇率。n 是第一种货币,m 是第二种货币。使用这两个 int 您可以追溯正确的汇率。
The diagonal in the matrix would be 1 (since USD >> USD =1 ).
矩阵中的对角线将为 1(因为 USD >> USD =1 )。
Finally, write a function to compute the exchange rate and return the corresponding text (you can use an hashmap for that, with the int as a key and the name ( string) as the value)
最后,编写一个函数来计算汇率并返回相应的文本(您可以使用哈希映射,以 int 作为键,名称(字符串)作为值)
exchange_rate = currency[1][2];
HashMap hm = new HashMap();
hm.put(1, new string("USD");
etc...
等等...
回答by DaveB
Express all of your rates as a multiplier of one standard value, for instance use the USD as the standard value. Then the conversion values for GBP would be 1.60, the USD would be 1.0 and the Euro would be 1.29. Then the conversion calculation would be:
将您的所有费率表示为一个标准值的乘数,例如使用美元作为标准值。那么英镑的换算值为 1.60,美元为 1.0,欧元为 1.29。然后转换计算将是:
From Value * From Conversion * 1/To Conversion
从价值 * 从转化 * 1/到转化
For instance from 1 GBP to Euro would be:
例如,从 1 英镑到欧元将是:
1 * 1.60 * (1/1.29) = 1.24
1 * 1.60 * (1/1.29) = 1.24
If you store all of your rates in a HashMap then you can avoid the switch statements completely.
如果您将所有汇率存储在 HashMap 中,那么您可以完全避免使用 switch 语句。
回答by Tano
Why you are using this way to convert currency. You have the JSR 354 Money and Currency APIhere are some examples you can use, its really easy to use and fast:
为什么要使用这种方式来转换货币。您拥有JSR 354 Money and Currency API,这里有一些您可以使用的示例,它非常易于使用且快速:
MonetaryAmount?monetaryAmount?=?Money.of(100.20,?usd);
CurrencyUnit?currency?=?monetaryAmount.getCurrency();
NumberValue?numberValue?=?monetaryAmount.getNumber();
?
int?value=?numberValue.intValue();