开发者

unreachable statement

I have loop designed to validate the user input on a question, it was working fine until I added this;

        if (userInput.charAt(0) > NUMCOLS && userInput.charAt(0) < 0);
        {
            System.out.println("Error, " + userInput + " is an invalid move.");
            continue;
        }

before this

        if (userInput.charAt(2) !='-')
        {
     开发者_StackOverflow中文版       System.out.println("Error, " + userInput + " is an invalid move.");
            continue;
        }

Now whenever I try to compile I get an error stating that this is an unreachable statement, what is causing this?


There's a spurious ';' in the first line of your added code that makes the first continue; always execute!


your if test has an empty body!

so the code below is always executed and since there's a continue code the following instructions are never executed..


Both of those conditions cannot be true at the same time, and the compiler is aware of this.

Read it out loud :

If the user inputs first character is greater than NUMCOLS and its less than 0! If NUMCOLS is 0 or greater, the second condition cannot be true at the same time, and vice versa.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜