具有中断的无法访问的声明 [英] Unreachable Statement with Break

查看:150
本文介绍了具有中断的无法访问的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个上一个问题,但意识到我发布了错误的违规代码。我在下面标出了令人不快的陈述。

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; }

}


推荐答案

如果你输入返回,然后该函数在执行 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.

这篇关于具有中断的无法访问的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆