嘿,我是Android Studio的新手,我一直在尝试制作一个计算器应用程序,该应用程序最初可以运行,我记得在“取消”按钮中进行过更改,此后无论我按哪个按钮,结果始终为NaN,香港专业教育学院尝试过,只是想不通。请告诉我原因是什么?代码如下。
String str="\0";
Character operator;
Double operand1=Double.NaN;
Double operand2;
EditText numberview;
public void compute()
{
final EditText numberview = (EditText) findViewById(R.id.editText);
if(operand1!=Double.NaN)
{
operand2=Double.parseDouble(numberview.getText().toString());
switch(operator)
{
case '+': operand1=operand1+operand2; break;
case '-': operand1=operand1-operand2; break;
case '*': operand1=operand1*operand2; break;
case '/': operand1=operand1/operand2; break;
case '=': break;
default: break;
}
}
else
{
operand1=Double.parseDouble(numberview.getText().toString());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText numberview = (EditText) findViewById(R.id.editText);
Button number1 = (Button) findViewById(R.id.button1);
Button number2 = (Button) findViewById(R.id.button2);
Button number3 = (Button) findViewById(R.id.button3);
Button number4 = (Button) findViewById(R.id.button4);
Button number5 = (Button) findViewById(R.id.button5);
Button number6 = (Button) findViewById(R.id.button6);
Button number7 = (Button) findViewById(R.id.button7);
Button number8 = (Button) findViewById(R.id.button8);
Button number9 = (Button) findViewById(R.id.button9);
Button number0 = (Button) findViewById(R.id.button10);
Button numbercancel = (Button) findViewById(R.id.button11);
Button numberplus = (Button) findViewById(R.id.button12);
Button numberminus = (Button) findViewById(R.id.button13);
Button numberdivide = (Button) findViewById(R.id.button15);
Button numbermultiply = (Button) findViewById(R.id.button14);
Button numberequal = (Button) findViewById(R.id.button17);
number1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "1";
numberview.setText(str);
}
});
number2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "2";
numberview.setText(str);
}
});
number3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "3";
numberview.setText(str);
}
});
number4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "4";
numberview.setText(str);
}
});
number5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "5";
numberview.setText(str);
}
});
number6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "6";
numberview.setText(str);
}
});
number7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "7";
numberview.setText(str);
}
});
number8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "8";
numberview.setText(str);
}
});
number9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "9";
numberview.setText(str);
}
});
number0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = numberview.getText().toString() + "0";
numberview.setText(str);
}
});
numbercancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
str = "";
numberview.setText("");
operand1=Double.NaN;
}
});
numberplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operator = '+';
compute();
str="";
numberview.setText("");
}
});
numberminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operator = '-';
compute();
str="";
numberview.setText("");
}
});
numberdivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operator = '/';
compute();
str="";
numberview.setText("");
}
});
numbermultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operator = '*';
compute();
str="";
numberview.setText("");
}
});
numberequal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
compute();
numberview.setText(String.valueOf(operand1));
}
});
}
答案 0 :(得分:0)
我找到了解决方案的人,我对NaN进行了一些搜索,我只需要使用Double.IsNan(operand1)函数而不是我使用的函数。 干杯。