Unreachable Statement with Break(带中断的无法访问的语句)
问题描述
所以我有一个先前的问题,但意识到我发布了错误的违规代码.我已经在下面标记了冒犯性的陈述.
So I had a previous question but realized I posted the wrong offending code. I've marked the offending statements below.
我要做的是使用该 switch 语句为每个运算符设置优先级.
What I am trying to do is set the precedence for each of the operators with that switch statement.
也许有人可以为我指明正确的方向.
Maybe someone could point me in the right direction.
请注意,我正在运行 JAVA 7,因此 String Switch 可以工作.
Just as a note, I AM running JAVA 7 so String Switch will work.
代码
opType.java
opType.java
import java.io.*;
public final class opType {
public static opType ADD = new opType( "Add" );
public static opType SUB = new opType( "Sub" );
public static opType MULT = new opType( "Mult" );
public static opType DIV = new opType( "Div" );
public static opType MOD = new opType( "Mod" );
public static opType LPAR = new opType( "LParen" );
public static opType RPAR = new opType( "RParen" );
protected String name;
private opType( String n )
{
name = n;
}
public String getName()
{
return name;
}
Operator.java
Operator.java
public class Operator extends Token {
protected opType val;
public boolean isOperator() { return true; }
public boolean isOperand() { return false; }
protected int getPrec()
{
switch(val.getName())
{
case "LParen":
{
return 0;
break; //unreachable
}
case "RParen":
{
return 0;
break; //unreachable
}
case "Mult":
{
return 1;
break; //unreachable
}
case "Div":
{
return 1;
break; //unreachable
}
case "Mod":
{
return 1;
break; //unreachable
}
case "Add":
{
return 2;
break; //unreachable
}
case "Sub":
{
return 2;
break; //unreachable
}
}
return 0;
}
public static int compare( Operator a, Operator b )
{
if( a.getPrec() == b.getPrec() )
return 0;
else if( a.getPrec() < b.getPrec() )
return -1;
else
return 1;
}
public opType getVal() { return val; }
public Operator( opType v ) { val = v; }
}
推荐答案
如果你放了一个return,那么函数会在break执行之前返回,因此break 永远不会到达.
If you put a return, then the function returns before the break is executed and therefore the break will never be reached.
相反,您可以使用设置为所需值的变量,并在切换后返回该变量.或者干脆去掉 break 语句.
Instead you could use a variable that you set to a desired value and after the switch return that. Or just get rid of the break statements.
这篇关于带中断的无法访问的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带中断的无法访问的语句
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
