double Euro = 1.37;
double USAD = 1.81;
double JapYen = 190.00;
double Zloty = 5.88;
//First menu choice screen
if (MenuChoice == 1) {
System.out.println("Which of the following currencies do you wish to exchange into sterling?");
System.out.println("Euro - EUR");
System.out.println("USA Dollar - USD");
System.out.println("Japanese Yen - JPY");
System.out.println("Polish Zloty - PLN");
System.out.print("Please enter the three letter currency: ");
//Currency input validation
String CurChoice = "";
boolean isCorrectCurrency = false;
do {
CurChoice = keybStr.next();
isCorrectCurrency = CurChoice.matches("^EUR|USD|JPY|PLN$");
if (isCorrectCurrency) {
System.out.println("");
} else {
System.out.print("Invalid Currency Entered. Please Retry: ");
}
} while (!isCorrectCurrency);
//Exchange amount calculator
System.out.print("Enter the amount you wish to exchange of " + CurChoice + ": ");
double ExcAmount = keybStr.nextInt();
if (CurChoice == "EUR") {
double result = ExcAmount / Euro;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice == "USD") {
double result = ExcAmount / USAD;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice == "JPY") {
double result = ExcAmount / JapYen;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice == "PLN") {
double result = ExcAmount / Zloty;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else {
System.out.println("");
}
}//if
所以这就是我到目前为止所得到的。我希望代码说(例如)“500.00 in EUR =£364.96”,但是当我运行代码时,它只是在用户输入他们希望使用的货币后结束。有帮助吗?我不确定是否需要将if语句放在循环或其他内容中。
答案 0 :(得分:1)
如this answer中所述,使用==
进行字符串检查,如果它们是同一个项目。在这种情况下,即使CurChoice
和其中一个文字字符串"EUR"
可能具有相同的值,它仍然是 false ,因为这是比较引用。所以发生的事情是它们都是假的,系统会从else
打印出一个空白行。要解决此问题,请使用.equals()
进行比较。
if (CurChoice.equals("EUR")) {
double result = ExcAmount / Euro;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice.equals("USD")) {
double result = ExcAmount / USAD;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice.equals("JPY")) {
double result = ExcAmount / JapYen;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else if (CurChoice.equals("PLN")) {
double result = ExcAmount / Zloty;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
} else {
System.out.println("");
}
答案 1 :(得分:0)
如果您使用的是Java 1.7+,那么切换也可以:
switch (CurChoice) {
case "EUR":
double result = ExcAmount / Euro;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
break;
case "USD":
double result = ExcAmount / USAD;
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
break;
...
default:
System.out.println("");
}
但最好的方法是将所有货币代码和汇率设置为地图,例如
HashMap<String, Double> = new HashMap<>();
curMap.put("EUR", Euro);
curMap.put("USD", USAD);
...
Double rate = curMap.get(CurChoice);
if (rate == null) {
System.out.println("");
} else {
double result = ExcAmount / Double.valueOf(rate);
System.out.println(ExcAmount + " in " + CurChoice + " = £" + result);
}