我正在使用两个textview(一个用于公式,一个用于结果)构建此计算器应用程序。现在计算后的结果存储在名为result的变量中,默认情况下该变量为空。但是当我运行它时,在函数执行后,一切正常但结果变量仍然是一个空字符串。请帮帮我。
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;
}
}
答案 0 :(得分:1)
您有两个结果变量:result
和resultdisplay
。计算代码更新result
,但在updateresultdisplay()
范围内,您可以使用显示(不变)resultdisplay
的内容。
因此,您只需将updateresultdisplay()
更新为
private void updateresultdisplay() {
resultscreen.setText(result);
}