大家好,我有这个Android计算器,它包含2个文本视图命名,计算器(所有计算发生,visibilty设置为无敌)和texview(显示方程)。算术运算符似乎运行良好,例如3 + 3 + 5 = 11,5 + 5-1 = 9,5 / 5-1 = 0。我遇到的问题是,无论何时方程看起来像上面的例子,它给出了正确的答案,但当我有3-2 + 1,5,5 + 1,2 + 2/4的东西时,我得到了非常错误的答案。相关按钮的代码是......
button1.Click += delegate {
textveiw.Text += button1.Text;
calculator.Text += button1.Text;
if (equalToClick==true){
total1=0;
total2=0;
calculator.Text="";
calculator.Text += button1.Text;
textveiw.Text=button1.Text;
equalToClick=false;
}
};
plus.Click += delegate {
textveiw.Text += plus.Text;
total1=total1+double.Parse
(calculator.Text);
calculator.Text=("");
equalToClick=false;
operatorSelected="+";
plusClick=true;
};
sub.Click += delegate {
textveiw.Text += sub.Text;
total1=total1+double.Parse
(calculator.Text);
calculator.Text=("");
equalToClick=false;
operatorSelected="-";
subClick = true;
};
multiplication.Click += delegate {
textveiw.Text += multiplication.Text;
total1= total1+double.Parse
(calculator.Text);
calculator.Text=("");
equaltoclick=false;
operatorSelected="*";
mulClick = true;
};
divide.Click += delegate {
textveiw.Text += divide.Text;
total1 = total1 + double.Parse
(calculator.Text);
calculator.Text=("");
equalToClick=false;
operatorSelected="/";
divClick = true;
};
//equals button
equalto.Click+=delegate {
switch(operatorSelected)
{
case "+" :
total2=total1+double.Parse
(calculator.Text);
textveiw.Text=total2.ToString();
equalToClick=true;
plusClick=false;
break;
case "-":
total2=total1 - double.Parse
(calculator.Text);
textveiw.Text=total2.ToString();
equalToClick=true;
subClick=false;
break;
case "*":
total2=total1 * double.Parse
(calculator.Text);
textveiw.Text=total2.ToString();
equalToClick=true;
mulClick=false;
break;
case "/":
total2=total1 / double.Parse
(calculator.Text);
textveiw.Text=total2.ToString();
equalToClick=true;
divClick=false;
break;
default :
if(subClick==false &&
plusClick==false &&
mulClick==false &&
divClick==false){
Toast.MakeText
(this,"please select an
operator",ToastLength.Short)
.Show();
}
break;
}
};
cancel.Click += delegate {
textveiw.Text=("");
calculator.Text=("");
total1=0;
total2=0;
};