【发布时间】:2013-11-23 00:14:37
【问题描述】:
所以我有一个 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;
}
}
【问题讨论】:
-
您使用的是 Java 7 吗?否则你不能对字符串进行切换。您能否更清楚地说明哪些情况无法访问?
-
你是用java 7编译的吗
-
您的意思是注释掉 break 语句吗?如果是违规者,请将
//unreachable放在案例陈述中 -
你的意思是你没有那里有休息或休息不可达?通过注释掉自己的休息就不清楚了。
-
我似乎无法重现此问题。我嘲笑了
Operand、Operator、Token和opType,一切都编译得很好。
标签: java switch-statement break