首先使用eclipse。我正在构建一个Android计算器。我有我的所有代码,除了有问题。我的一个变量没有被使用。我的total1变量基本上是程序的计数器。当我添加两个数字并按下等号时,它总是打印“0.0”,这是total1的值。这意味着由于某种原因,它在整个计划中从未改变过。我甚至使用print语句对此进行了测试。我更改了total1的值,它将打印我指定给它的值。可能是什么问题?
public class HelloAndroidActivity extends Activity {
public EditText display;
String display1;
double displayValue;
// Program is printing out total1, as if it weren't maninpulated in the code
double total1 = 0.0;
double total2 = 0.0;
char theOperator;
public String buttonText;
public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display = (EditText) findViewById(R.id.editText1);
// Could it have something to do with this if statement?
if (display.length() != 0) {
display1 = display.getText().toString();
displayValue = Double.parseDouble(display1);
}
}
//Get the operator of the multiply, divide, subtract, add buttons when they are clicked and then do the following
public void getOperator(String btnText) {
theOperator = btnText.charAt(0);
total1 += displayValue;
display.setText("");
}
// Display a string in the box when a number button is clicked
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
total2 = 0.0;
display.setText("");
break;
case R.id.bAdd:
buttonText = "+";
ButtonAdd = (Button) findViewById(R.id.bAdd);
ButtonAdd.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMinus:
buttonText = "-";
ButtonMinus = (Button) findViewById(R.id.bMinus);
ButtonMinus.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bMultiply:
buttonText = "*";
ButtonMultiply = (Button) findViewById(R.id.bMultiply);
ButtonMultiply.setText(buttonText);
getOperator(buttonText);
break;
case R.id.bDivide:
buttonText = "/";
ButtonDivide = (Button) findViewById(R.id.bDivide);
ButtonDivide.setText(buttonText);
getOperator(buttonText);
break;
// Here's the equals button. When I click it it prints out "0.0", the value of total1, instead of adding the total below
case R.id.bEqual:
switch (theOperator) {
case '+':
total2 = total1 + displayValue;
break;
case '-':
total2 = total1 - displayValue;
break;
case '*':
total2 = total1 * displayValue;
break;
case '/':
total2 = total1 / displayValue;
break;
}
display.setText(Double.toString(total2));
total1 = 0.0;
break;
}
}
}
答案 0 :(得分:1)
我只看到一个位置,其中total1
变量已被修改,并且其中添加了displayValue
的值。 displayValue
只在onCreate
方法(每个活动生命周期只调用一次)中修改一次,editText1
控件的内容加载total1
控件(我们没有源代码)对)。
所以简而言之,我并不感到惊讶{{1}}没有被修改。这是检查应用程序的整个逻辑流程的问题。
答案 1 :(得分:1)
您正尝试在get运算符方法中添加displayValue
,但您尚未更改此值。在将其添加到total1
之前,您应该从显示中削减此值。它应该是这样的:
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
total1+=Double.parseDouble(display.getText().ToString());
display.setText("");
}
如果您需要保存当前显示值,可以这样:
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
displayValue = Double.parseDouble(display.getText().ToString());
total1+=displayValue;
display.setText("");
}