我想制作一个堆栈计算器应用程序。但当我输入3 4 +时,我的应用程序崩溃并抛出EmptyStackException。
public class Calculator { // RechenLogik
private Stack<Double> stack = new Stack<Double>();
private MainActivity activity ;
public Calculator(MainActivity activity ) {
this.activity = activity;
}
public void calculate() {
String expr =activity.getTextfromTextField();
switch (expr) {
case "+":
stack.push(stack.pop() + stack.pop());
activity.setTextField(stack.peek().toString()); // set Text
break;
default:
stack.push(Double.parseDouble(expr));
break;
}
}
}
And in Main Activity in OnClick Method
case R.id.button17: r.calculate(); TextField.setText(""); break; // Enter
press
任何帮助都将不胜感激。