不确定如何计算所述变量


    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 欧元 = 364.96 英镑",尽管当我运行代码时,它只是在用户输入他们希望使用的货币后结束。有什么帮助吗?我不确定我是否需要将 if 语句放在循环中或其他东西中。

如本答案中所述,使用 == for 字符串检查它们是否是同一项目。在这种情况下,即使CurChoice和其中一个文本字符串"EUR"可能具有相同的值,它仍然是的,因为这是比较引用。所以发生的事情是它们都是假的,系统从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 1.7+,switch 也可以:

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

相关内容

最新更新