java 开关_java-开关中的不可达语句

因此,我有一个switch语句,它将打开数组中的每个字符串.当遇到运算符时,会将其添加到ArrayList中.但是,由于某种原因,当我编译代码时,它说某些语句“不可到达”.我在下面的代码中用“ //”标记了哪些语句是令人反感的.

提前致谢!

码:

import java.util.*;

import java.io.*;

public class a3

{

public static void main(String[] args) throws FileNotFoundException

{

ArrayList tokens = new ArrayList();

String[] readTokens;

Stack postStack = new Stack();

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 al)

{

return "";

}

public static int evalPostfix(String post)

{

return 0;

}

}

解决方法:

我在代码中添加了缺少的类/接口/枚举声明,并且无法重现该错误.问题显然出在您尚未发布的代码中.

这是适合我的编译方法:

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 tokens = new ArrayList();

String[] readTokens;

Stack postStack = new Stack();

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 al)

{

return "";

}

public static int evalPostfix(String post)

{

return 0;

}

}

标签:break,switch-statement,java

来源: https://codeday.me/bug/20191030/1965787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值