我正在尝试基于调车场算法来构造我自己的表达式评估器。不过,我在向堆栈中添加String对象时遇到了困难。我的代码每次都跳过这些行,即使满足了条件。有什么建议吗?我已将operatorStack声明为:
Stack operatorStack = new Stack();我在想这和我的if声明有关。我已经用Eclipse调试器测试了它,当currentChar变量显示为"("时,它仍然跳过推到堆栈的过程。
以下是有争议的节选:
int count = 0;
String currentChar=new String();
//While loop to test for the conditions stated in the Shunting-yard algorithm.
while (count<=temp.length()) {
currentChar = temp.substring(count, count+1);
if(currentChar == "(")
operatorStack.push(currentChar);
if(expressionEval(currentChar) instanceof Integer)
outputQueue.offer((Integer)expressionEval(currentChar));
if(currentChar == "+" ||
currentChar == "-" ||
currentChar == "<" ||
currentChar == "?") {
while(operatorStack.peek() == "+" ||
operatorStack.peek() == "-" ||
operatorStack.peek() == "<" ||
operatorStack.peek() == "?") {
outputQueue.offer((Integer)operatorStack.peek());
operatorStack.pop();
}
operatorStack.push(currentChar);
}发布于 2013-11-05 14:08:57
您正在使用"==“而不是"equals”比较字符串。不符合比较条件。
此外,在使用myString.charAt(计数)(例如)查看单个字符时,应该考虑使用"char“而不是"String”。
-这是在考虑你是在用Java来尝试这个.
https://stackoverflow.com/questions/19790889
复制相似问题