(Java,Netbeans)我的计算器应用程序在不应该返回零值时返回了零值。

huangapple 未分类评论46阅读模式
英文:

(Java, Netbeans) My calculator application keeps returning a zero value when it shouldn't

问题

我已经创建了一个货币转换计算器。然而,由于我使用的API不支持某些非洲货币,我在代码中硬编码了它们的汇率。

以下是一个转换的示例。工作原理是我从两个组合框中获取fromCurr和toCurr。如果组合框检测到选择了四种非洲货币之一(埃及镑、尼日利亚奈拉、阿尔及利亚第纳尔和摩洛哥迪拉姆),应用程序应该使用硬编码的值。否则,应用程序应该使用我编写的ExchangeRate类。

ExchangeRate er = new ExchangeRate();
        
double result = 0;
        
if ((fromCurr != "Egyptian Pound" && fromCurr != "Nigerian Naira" && fromCurr != "Algerian Dinar" && fromCurr != "Moroccan Dirham") &&
    (toCurr != "Egyptian Pound" && toCurr != "Nigerian Naira" && toCurr != "Algerian Dinar" && toCurr != "Moroccan Dirham")) {
    try {
        String exRate = er.getExRate(fromCurr, toCurr);
        double exRateAmnt = Double.parseDouble(exRate);
        result = Double.parseDouble(txfEq.getText()) * exRateAmnt;
    } catch (IOException fe) {
        System.out.println(fe);
    } catch (NumberFormatException nf) {
        // If the text field is empty, output an error
        txfDisp.setText("ERR");
    }
} else if ((fromCurr == "Egyptian Pound" || fromCurr == "Nigerian Naira" || fromCurr == "Algerian Dinar" || fromCurr == "Moroccan Dirham") ||
           (toCurr == "Egyptian Pound" || toCurr == "Nigerian Naira" || toCurr == "Algerian Dinar" || toCurr == "Moroccan Dirham")) {
    if (fromCurr == "Egyptian Pound") {
        if (toCurr == "United States Dollar") {
            result = Double.parseDouble(txfEq.getText()) * 0.063;
        } else if (toCurr == "Euro") {
            result = Double.parseDouble(txfEq.getText()) * 0.058;
        } else if (toCurr == "Japanese Yen") {
            result = Double.parseDouble(txfEq.getText()) * 6.77;
        } else if (toCurr == "British Pound") {
            result = Double.parseDouble(txfEq.getText()) * 0.051;
        } else if (toCurr == "Australian Dollar") {
            result = Double.parseDouble(txfEq.getText()) * 0.099;
        } else if (toCurr == "Swiss Franc") {
            result = Double.parseDouble(txfEq.getText()) * 0.061;
        } else if (toCurr == "New Zealand Dollar") {
            result = Double.parseDouble(txfEq.getText()) * 0.11;
        } else if (toCurr == "Canadian Dollar") {
            result = Double.parseDouble(txfEq.getText()) * 0.089;
        } else if (toCurr == "South Korean Won") {
            result = Double.parseDouble(txfEq.getText()) * 77.65;
        } else if (toCurr == "Mexican Peso") {
            result = Double.parseDouble(txfEq.getText()) * 1.56;
        } else if (toCurr == "Russian Ruble") {
            result = Double.parseDouble(txfEq.getText()) * 4.77;
        } else if (toCurr == "Turkish Lira") {
            result = Double.parseDouble(txfEq.getText()) * 0.45;
        } else if (toCurr == "UAE Dirham") {
            result = Double.parseDouble(txfEq.getText()) * 0.23;
        } else if (toCurr == "South African Rand") {
            result = Double.parseDouble(txfEq.getText()) * 1.18;
        } else if (toCurr == "Egyptian Pound") {
            result = Double.parseDouble(txfEq.getText()) * 1;
        } else if (toCurr == "Nigerian Naira") {
            result = Double.parseDouble(txfEq.getText()) * 24.7;
        } else if (toCurr == "Algerian Dinar") {
            result = Double.parseDouble(txfEq.getText()) * 8.13;
        } else if (toCurr == "Moroccan Dirham") {
            result = Double.parseDouble(txfEq.getText()) * 0.62;
        }

非洲货币与另一种非洲货币之间的转换是有效的。非非洲货币与另一种非非洲货币之间的转换也有效。然而,在将非洲货币转换为非非洲货币以及反之的情况下出现问题。无论我尝试转换什么初始值,输出都是零。

我怀疑问题要么出现在我的if/else语法上,但这不应该是问题,因为非洲与非洲之间的转换是有效的,要么就是因为非非洲货币在主if和else if中都被使用了,但这也不应该是问题。我只是试图找出问题出在哪里,但仍然无法找出问题所在。

英文:

I have created a currency conversion calculator. However, since the API I am using doesn't support certain African currencies I am hard-coding their exchange rates in.

The following is a sample of one of the conversions. The way it works is I get fromCurr and toCurr from two combo boxes. If the combo boxes detect one of the four African currencies selected (Egyptian Pound, Nigerian Naira, Algerian Dinar and Moroccan Dirham) the application should use the hard-coded values. Otherwise, the application should make use of the ExchangeRate class I have coded.

ExchangeRate er = new ExchangeRate();
    
    double result = 0;
    
    if ((fromCurr != "Egyptian Pound" && fromCurr != "Nigerian Naira" && fromCurr != "Algerian Dinar" && fromCurr != "Moroccan Dirham") && (toCurr != "Egyptian Pound" && toCurr != "Nigerian Naira" && toCurr != "Algerian Dinar" && toCurr != "Moroccan Dirham")) {
        try {
            String exRate = er.getExRate(fromCurr, toCurr);
            double exRateAmnt = Double.parseDouble(exRate);
            result = Double.parseDouble(txfEq.getText())*exRateAmnt;
        } catch (IOException fe) {
            System.out.println(fe);
        } catch (NumberFormatException nf) {
            // If the text field is empty, output an error
            txfDisp.setText("ERR");
        }
    } else if ((fromCurr == "Egyptian Pound" || fromCurr == "Nigerian Naira" || fromCurr == "Algerian Dinar" || fromCurr == "Moroccan Dirham") || (toCurr == "Egyptian Pound" || toCurr == "Nigerian Naira" || toCurr == "Algerian Dinar" || toCurr == "Moroccan Dirham")) {
        if (fromCurr == "Egyptian Pound") {
            if (toCurr == "United States Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.063;
            } else if (toCurr == "Euro") {
                result = Double.parseDouble(txfEq.getText())*0.058;
            } else if (toCurr == "Japanese Yen") {
                result = Double.parseDouble(txfEq.getText())*6.77;
            } else if (toCurr == "British Pound") {
                result = Double.parseDouble(txfEq.getText())*0.051;
            } else if (toCurr == "Australian Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.099;
            } else if (toCurr == "Swiss Franc") {
                result = Double.parseDouble(txfEq.getText())*0.061;
            } else if (toCurr == "New Zealand Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.11;
            } else if (toCurr == "Canadian Dollar") {
                result = Double.parseDouble(txfEq.getText())*0.089;
            } else if (toCurr == "South Korean Won") {
                result = Double.parseDouble(txfEq.getText())*77.65;
            } else if (toCurr == "Mexican Peso") {
                result = Double.parseDouble(txfEq.getText())*1.56;
            } else if (toCurr == "Russian Ruble") {
                result = Double.parseDouble(txfEq.getText())*4.77;
            } else if (toCurr == "Turkish Lira") {
                result = Double.parseDouble(txfEq.getText())*0.45;
            } else if (toCurr == "UAE Dirham") {
                result = Double.parseDouble(txfEq.getText())*0.23;
            } else if (toCurr == "South African Rand") {
                result = Double.parseDouble(txfEq.getText())*1.18;
            } else if (toCurr == "Egyptian Pound") {
                result = Double.parseDouble(txfEq.getText())*1;
            } else if (toCurr == "Nigerian Naira") {
                result = Double.parseDouble(txfEq.getText())*24.7;
            } else if (toCurr == "Algerian Dinar") {
                result = Double.parseDouble(txfEq.getText())*8.13;
            } else if (toCurr == "Moroccan Dirham") {
                result = Double.parseDouble(txfEq.getText())*0.62;
            }

Conversions between an African currency and another African currency work. Conversions between a non-African currency and another non-African currency work. However, the problem arises when converting from an African currency to a non-African currency and vice-versa. The output turns up as zero no matter what initial value I try to convert.

I suspect this is a problem either with my if/else syntax, which shouldn't be the case since the African-African conversion works, or it could be with the fact that the non-African currencies are used in both the main if and the else if, but this shouldn't be the case either. Just trying to figure it out, but I still cannot figure out where the problem lies.

huangapple
  • 本文由 发表于 2020年5月4日 22:45:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61594979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定