我正在用 Java 为 Android 制作一个基本的计算器。我被困在最后一步,它必须将输入与前一个输入相加。看一下代码:
public void displayValue(){
String mything = display.getText().toString();
input = Integer.parseInt(mything);
}
public void number1(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(1));
displayValue();
}
public void number2(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(2));
displayValue();
}
public void plus(View view){
displayValue(); //result= 0
result = result + input; //result= input
input = 0; //input=0
//in this step input becomes 0 to let the user enter new number input but this
//input never add the old result and the equal shows the old result.
}
public void equal(View view){
displayValue();
display.setText(Integer.toString(result));
}
我注意到,如果我以相等的方法添加一行并将结果添加到输入中,我会得到正确的答案,但这不会有帮助,因为也会有减号按钮。
有任何想法吗?
谢谢。