繁体   English   中英

if语句中无法访问的代码

[英]Unreachable code in an if statement

public class Sorting {
String value;
char[] string;
Node nude;
Node current = nude.getNext();

public Sorting(String value) {
    this.value = value;
    string = value.toCharArray();
    token(string);
}

private void token(char[] string) {
    boolean isNumber = false;
    int trueCoefficient;
    int truePower;
    String temp;
    String coefficient = "";
    String power = "";
    int coefficientSignCounter = 0;
    int coefficientSign = 1;
    for (int i = 0; i < string.length; i++) {
        if (string[i] == '+' || string[i] == '-') {
            for (int j = i; true; j++) {
                if (string[j] == 'i')
                    coefficientSign++;
                if (coefficientSignCounter % 2 != 0)
                    coefficientSign = -1;
                if (Character.isDigit(string[j]))
                    i=j-1;
                    break;
            }

        }

        else if(string[i]=='x')
            trueCoefficient=1;

        else if (Character.isDigit(string[i])) {
            for (int j = i; true; j++)
                if (Character.isDigit(string[j]))
                    coefficient = coefficient
                            + Character.toString(string[j]);
            trueCoefficient = Integer.parseInt(coefficient) * coefficientSign;  //Error: Unreachable code
        }

    }
}
}

我还没有准备好使用我的代码,但无法摆脱此错误。 有人可以帮忙吗? 我正在尝试编写一个代码,如果有人有更多的主意,我想使用一个新的主意,那么我可以使用链接列表读取该公式并按幂对其进行排序。谢谢!

您在循环内声明了if(condition)但在循环外使用了elseif(condition)您的elseif条件无法到达

应该是这样

for(int i=0;i<someCondition;i++){//start of Block

   if(condition){

    }
    else if(condition){

   }
}  // End of Loop
-------------------->    you put elseif(condition) after this loop

因此得到Unreachable Code

注意: elseif()条件应紧随其后的是if子句

您将在另一个位置收到错误

for (int j = i; true; j++)-----> Infinite Loop 
if (Character.isDigit(string[j]))---> So only this will execute
coefficient = coefficient+ Character.toString(string[j]);
trueCoefficient = Integer.parseInt(coefficient) * coefficientSign;  //Error: Unreachable code

由于无限循环,Last语句将永远不会执行,这就是为什么Compiler会给您带来无法到达的代码错误

您没有条件终止最后一个for循环。 它是无限的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM