函数进程

时间:2016-06-01 19:47:35

标签: java android

我正在使用两个textview(一个用于公式,一个用于结果)构建此计算器应用程序。现在计算后的结果存储在名为result的变量中,默认情况下该变量为空。但是当我运行它时,在函数执行后,一切正常但结果变量仍然是一个空字符串。请帮帮我。

ScientificCalculator.java

 int prevAnswer = 0;
TextView formulascreen;
TextView resultscreen;
String formuladisplay = "";
String resultdisplay = "";
String result = "";
String operator;

public void onClickEquals(View view) {

    Toast toast = Toast.makeText(getContext(), "Equals to: " + String.valueOf(result), Toast.LENGTH_SHORT);
    toast.show();

    if (operator == "+" || operator == "-" || operator == "x" || operator == "÷") {

        getResult();
        updateresultdisplay();

    }

}

private boolean getResult() {

    if (operator == "") return false;

    String[] operation = formuladisplay.split(Pattern.quote(operator));

    if (operation.length < 2) return false;

    result = String.valueOf(res.format(simpleOp(operation[0], operation[1], operator)));

    return true;

}

public double simpleOp(String a, String b, String op) {

    switch (op) {

        case "+":
            return Double.valueOf(a) + Double.valueOf(b);

        case "-":
            return Double.valueOf(a) - Double.valueOf(b);

        case "x":
            return Double.valueOf(a) * Double.valueOf(b);

        case "÷":
            return Double.valueOf(a) / Double.valueOf(b);

        default:
            return -1;

    }

}

1 个答案:

答案 0 :(得分:1)

您有两个结果变量:resultresultdisplay。计算代码更新result,但在updateresultdisplay()范围内,您可以使用显示(不变)resultdisplay的内容。

因此,您只需将updateresultdisplay()更新为

即可
private void updateresultdisplay() {
    resultscreen.setText(result);
}