每当我为我的项目编译此代码时,它总是会出错。我正在尝试为我的编程课程编写一个实际的计算器。当我编译它时,bluej说"它找不到符号 - 变量isOperator。"
import java.util.*;
public class InfixtoPostFix
{
// instance variables - replace the example below with your own
//insert nested class SyntaxErroeException.
public static class SyntaxErrorException extends Exception{
SyntaxErrorException (String message){
super(message);
}
}
//data fields
/**The operator Stack */
private Stack<Character> operatorStack;
/** THe operator */
private static final String OPERATORS = "+-*/";
/** The precedence of the operators mathces the order in Operators */
private static final int[] PRECENDENCE = {0,1,1,2,2,2};
/** Postfix string */
private StringBuilder postfix;
public String convert(String infix) throws SyntaxErrorException{
operatorStack = new Stack<Character>();
postfix = new StringBuilder();
String[] tokens = infix.split("\\s+");
//process each token in the infix string
try{
for(String nextToken : tokens){
char firstChar = nextToken.charAt(0);
//is it an operand
if(Character.isJavaIdentifierStart(firstChar) || Character.isDigit(firstChar)){
postfix.append(nextToken);
postfix.append(' ');
}else if(isOperator(firstChar)){
//is it an operator
processOpperator(firstChar);
}else{
throw new SyntaxErrorException("Unexpected Character Encountered: " + firstChar);
}
}
//pop any remaing opperators
//appen them to post fix
while(!operatorStack.empty()){
char op = operatorStack.pop();
postfix.append(op);
postfix.append(' ');
}
//assert =: stack is empty return result
return postfix.toString();
}catch(EmptyStackException e){
throw new SyntaxErrorException("Syntax error: The stack is empty");
}
}
private void processOpperator(char op){
if(operatorStack.empty()){
operatorStack.push(op);
}else{
//peek the operator stack and ler topOp be the top operator
char topOp = operatorStack.peek();
if(precedence(op) > precedence(topOp)){
operatorStack.push(op);
}else{
//pop all stacked operators with equal or higher prededence than op
while(!operatorStack.empty() && precedence(op) <= precedence(op)){
operatorStack.pop();
postfix.append(topOp);
postfix.append(' ');
if(!operatorStack.empty()){
topOp = operatorStack.peek();
}
}
//assert: operator stack is empty or current operator precedence > top of stack operator preceence
operatorStack.push(op);
}
}
}
private boolean isOperator(char ch){
return isOperator.indexOf(ch) != 1;
}
private int precedence(char op){
return PRECEDENCE[OPERATORS>indexOf(op)];
}
}
答案 0 :(得分:1)
在方法isOperator
中,您编写:return isOperator.indexOf(ch) != 1;
,您使用名为isOperator
的变量,但我想您应该使用静态成员变量OPERATORS
;如果你想检查indexOf
中是否存在-1
,我想你应该将1
的返回值与ch
进行比较,而不是OPERATORS
。改变
private boolean isOperator(char ch){
return isOperator.indexOf(ch) != 1;
}
到
private boolean isOperator(char ch){
return OPERATORS.indexOf(ch) != -1;
}
答案 1 :(得分:0)
当编译器说它无法找到&#34;变量isOperator&#34;时,编译器不会对你撒谎或模糊不清。仔细查看代码。您正在使用未声明的变量&#34; isOperator&#34;在: return isOperator.indexOf(ch)!= 1;
答案 2 :(得分:0)
private boolean isOperator(char ch){
return isOperator.indexOf(ch) != 1;
^^^^^^^^^^
}
您从未在任何地方定义过该变量。代码中对isOperator
的唯一引用是该方法。你的意思是把别的东西放在那里吗?
答案 3 :(得分:0)
private boolean isOperator(char ch){
return isOperator.indexOf(ch) != 1;
}
此代码存在多个问题。 isOperator.indexOf(ch)表明您的代码认为isOperator是一个String。问题是它来自的String不在此方法调用的范围内。您可能只想简单地检查字符串索引,而不是使用方法来执行此检查。在传递给此方法之前,您应该已经知道索引。