error at java.util.Stack.peek/pop(Unknown Source) with if el



我有一个任务来制作一个堆栈计算器,这就是我得到的:

public static void main(String[]args) {
ArrayList<String> commands = new ArrayList<String>();
commands.add("1");
commands.add("2");
commands.add("3");
commands.add("4");
commands.add("+");
commands.add("+");
commands.add("+");
commands.add("2");
commands.add("*");
commands.add("=");
System.out.println(commands); //prints the list
System.out.println(computeResult(commands));
commands.clear();
commands.add("2");
commands.add("4");
commands.add("/");
commands.add("=");
System.out.println(commands); //prints the list
System.out.println(computeResult(commands)); // gives 2
commands.clear();
commands.add("3");
commands.add("4");
commands.add("-");
commands.add("=");
System.out.println(commands); //prints the list
System.out.println(computeResult(commands)); // gives 1
}
public static int computeResult(ArrayList<String> commands) {
Stack<Integer> dataStack = new Stack<Integer>();
Iterator<String> commandsIterator = commands.iterator();
while(commandsIterator.hasNext()) {
/* sums up the two top elements from the stack and pushes the solution back on the stack
* @param int A, int B, int C
* @return
*/
if(commands.contains("+")) {
int A = dataStack.pop();
int B = dataStack.pop();
int C = A + B;
dataStack.push(C);
}
/* subtract the two top elements from the stack and pushes the solution back on the stack
* @param int A, int B, int C
* @return
*/
else if(commands.contains("-")) {
int A = dataStack.pop();
int B = dataStack.pop();
int C = B - A;
dataStack.push(C);
}
/* multiplies the two top elements from the stack and pushes the solution back on the stack
* @param int A, int B, int C
* @return
*/
else if(commands.contains("*")) {
int A = dataStack.pop();
int B = dataStack.pop();
int C = A * B;
dataStack.push(C);  
}
/* divides the two top elements of the stack and pushes the solution back on the stack
* @param int A, int B, int C
* @return
*/
else if(commands.contains("/")) {
int A = dataStack.pop();
int B = dataStack.pop();
int C = B / A;
dataStack.push(C);
}
/* pops the top element
* @return popped element
*/
else if(commands.contains("=")) {
return dataStack.peek();
}
/* if the command is a number it will be assigned to an integer and pushed on the stack
* @param int i, String command
* @return
*/
else {
String command = commandsIterator.next();
int i = Integer.parseInt(command);
dataStack.push(i);
}
}
return 0;
}

但问题是我得到上述错误作为输出。 在这个程序中,我使用 if else 来检查我的输入是运算符还是数字,如果它是一个数字,那么我按下数字,如果是运算符,我会弹出两个顶部元素并进行计算。结果再次被推动,所以我认为这应该不是问题。 我知道这个错误意味着什么,所以我的问题是如何在这种特殊情况下解决它。

我认为你在这里犯了一个错误

command.contains("+"(

我认为您的想法迭代到所有computeResult项目以检查该项目是运算符还是数字,对吧? 但是,如果您调用commands.contains("+")函数,则无论您的commands包含+字符,无论+处于什么位置,这将始终返回 true。

所以我的小固定是

while (commandsIterator.hasNext()) {
String s = commandsIterator.next();
/* sums up the two top elements from the stack and pushes the solution back on the stack
* @param int A, int B, int C
* @return
*/
if (s.equals("+")) {
...
else if (s.equals("-"))
...
else {
int i = Integer.parseInt(s);
dataStack.push(i);
}
}

我试过了。像冠军:)一样工作希望对:)有所帮助

你会得到一个错误,因为你的 computeResult 实现会检查 "command"-list 是否"包含"这些字符串。由于整个列表确实包含它,因此您尝试从堆栈中弹出不存在的项目。

简而言之,第一个 if 子句已经执行并导致错误。

尝试调试计算结果方法。

最新更新