所以我有一个switch语句,它将打开一个数组中的每个字符串。遇到运算符时,它会将其添加到ArrayList。但是,出于某种原因,当我编译代码时,它说某些语句是“无法访问”的。我在下面的代码中用“//”标记了哪些语句是令人讨厌的。
提前致谢!
代码:
import java.util.*;
import java.io.*;
public class a3
{
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Token> tokens = new ArrayList<Token>();
String[] readTokens;
Stack<Operator> postStack = new Stack<Operator>();
String filename = "input.infix";
String DELIM = " ";
File in = new File(filename);
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
readTokens = sc.nextLine().split(DELIM);
for (String s : readTokens)
{
switch(s)
{
case "(":
tokens.add(new Operator(opType.LPAR));
break;
case ")":
tokens.add(new Operator(opType.RPAR));
break; //unreachable
case "*":
tokens.add(new Operator(opType.MULT));
break; //unreachable
case "/":
tokens.add(new Operator(opType.DIV));
break; //unreachable
case "%":
tokens.add(new Operator(opType.MOD));
break; //unreachable
case "+":
tokens.add(new Operator(opType.ADD));
break; //unreachable
case "-":
tokens.add(new Operator(opType.SUB));
break; //unreachable
//Assuming the expression are valid (according to the
//assignment notes, anything other than operators are
//operands.
//
//NOTE: Even though spaces exist, they will not be
//interpreted as they are the delim
default:
tokens.add(new Operand(Integer.parseInt(s)));
break; //unreachable
}
}
String postfix = infix2postfix(tokens);
int finalResult = evalPostfix(postfix);
System.out.println(postfix + " = " + finalResult);
}
}
public static String infix2postfix(ArrayList<Token> al)
{
return "";
}
public static int evalPostfix(String post)
{
return 0;
}
}
答案 0 :(得分:3)
我在代码中添加了缺少的类/接口/枚举声明,无法重现错误。问题显然在您尚未发布的代码中。
以下是编译好的内容:
import java.util.*;
import java.io.*;
public class Test
{
public static interface Token
{
}
public static class Operator implements Token
{
public Operator(opType type)
{
}
}
public static enum opType
{
LPAR, RPAR, MULT, DIV, MOD, ADD, SUB
}
public static class Operand implements Token
{
public Operand(int val)
{
}
}
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Token> tokens = new ArrayList<Token>();
String[] readTokens;
Stack<Operator> postStack = new Stack<Operator>();
String filename = "input.infix";
String DELIM = " ";
File in = new File(filename);
Scanner sc = new Scanner(in);
while (sc.hasNextLine())
{
readTokens = sc.nextLine().split(DELIM);
for (String s : readTokens)
{
switch (s)
{
case "(":
tokens.add(new Operator(opType.LPAR));
break;
case ")":
tokens.add(new Operator(opType.RPAR));
break;
case "*":
tokens.add(new Operator(opType.MULT));
break;
case "/":
tokens.add(new Operator(opType.DIV));
break;
case "%":
tokens.add(new Operator(opType.MOD));
break;
case "+":
tokens.add(new Operator(opType.ADD));
break;
case "-":
tokens.add(new Operator(opType.SUB));
break;
// Assuming the expression are valid (according to the
// assignment notes, anything other than operators are
// operands.
//
// NOTE: Even though spaces exist, they will not be
// interpreted as they are the delim
default:
tokens.add(new Operand(Integer.parseInt(s)));
break;
}
}
String postfix = infix2postfix(tokens);
int finalResult = evalPostfix(postfix);
System.out.println(postfix + " = " + finalResult);
}
}
public static String infix2postfix(ArrayList<Token> al)
{
return "";
}
public static int evalPostfix(String post)
{
return 0;
}
}
答案 1 :(得分:1)
您的代码完全正常,建议购买其他人。
将break
置于语句swhich
锁定中是可以的。但是,休息应该是最后一个陈述,你可能已经忘记了一些案例陈述。