gpt4 book ai didi

java - 不确定如何计算所述变量

转载 作者:行者123 更新时间:2023-12-01 12:05:28 24 4
gpt4 key购买 nike

    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 语句放在循环或其他内容中。

最佳答案

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("");
}

关于java - 不确定如何计算所述变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27660297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com