gpt4 book ai didi

java - Android 计算器中不使用变量

转载 作者:行者123 更新时间:2023-12-02 07:57:01 24 4
gpt4 key购买 nike

首先使用 eclipse。我正在构建一个 Android 计算器。我有所有的代码,除了有问题。我的变量之一没有被使用。我的total1变量基本上是程序的计数器。当我将两个数字相加并按等于时,它总是打印“0.0”,这就是total1的值。这意味着由于某种原因,它在整个程序中从未改变。我什至使用打印语句对此进行了测试。我更改了 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;
}
}
}

最佳答案

您正在尝试在 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("");
}

关于java - Android 计算器中不使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9488320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com