简体   繁体   中英

Why does the statement after the comment Digit 2 is getting marked as “unreachable”?

In the following code, the statement after ''Digit 2 is marked as unreachable. Why?

while(true){
    System.out.println("Welcome to Pi Conquest!");
    System.out.println("Name as many digits of Pi as you can");
    System.out.println("If you get an error you restart");
    System.out.println("Only includes up to 100 digits of Pi");
    System.out.println("---------------------------------------");
    System.out.println("");

    Scanner keyboard = new Scanner(System.in);

    //Digit 1
    System.out.println("Stats:");
    System.out.println("Number of Digits Entered: 0");
    System.out.println("Digits Entered: 3.");
    System.out.println("Enter the first digit of Pi. (Starting with decimals)");

    int digit1 = keyboard.nextInt();
    if(digit1==1){
        System.out.println("Correct, enter the next digit.");
        continue;
    }else{
        System.out.println("Incorrect, restarting.");
        break;
    }

    //Digit 2     <--------------------------------------- next statement marked unreachable
    System.out.println("Stats:");
    System.out.println("Number of Digits Entered: 1");
    System.out.println("Digits Entered: 3.1");

    int digit2 = keyboard.nextInt();
    if(digit2==4){
        System.out.println("Correct, enter the next digit.");
        continue;
    }else{
        System.out.println("Incorrect, restarting.");
        break;
    }
}

Because in the previous if , either the branch that ends with continue or the branch that ends with break will be executed.

Both these instructions will cause the current iteration to end, so there is no way in which more code may be executed.

The first option in this statement leads to restarting the loop, and the second one - ends the loop and starts execution of code after the while loop. So, the next lines won't be reached.

if(digit1==1){
        System.out.println("Correct, enter the next digit.");
        continue;
    }else{
        System.out.println("Incorrect, restarting.");
        break;
    }

You are using continue and break wrong. Your break s should be continue s and your continue s should be simply removed.

if(digit1==1){
    System.out.println("Correct, enter the next digit.");
}else{
    System.out.println("Incorrect, restarting.");
    continue;
}

break exits the while(true) loop and since there's nothing else after that loop, your program ends. Instead, you want to start a new loop iteration at this point (and skip over the remaining of the current iteration), thus you need a continue .

As mentioned earlier, continue starts a new loop iteration. Instead, you want to simply go on with the current iteration. You don't need anything fancy for this: just let the if block do its thing and continue after the if...else statement.

Off-topic: you'll have quite a lot of code duplication if you continue in this fashion for the other digits. I suggest you look into making a for loop over the digits of pi so you can compare the "current digit" with the entered number.

Your first if statement either fails or continues the loop (it does not continue the "code"), so nothing after the first if statement will ever be executed.

You might want to consider coding your request differently (eg step through the digits of pi in a String answer, providing feedback to the user.

All code below //Digit 2 comment is unreachable. I think that you didn't get what continue statement means. When you run while loop it gets int from keyboard by

int digit1 = keyboard.nextInt();

and according to your code

if(digit1==1){
    System.out.println("Correct, enter the next digit.");
    continue;
}else{
    System.out.println("Incorrect, restarting.");
    break;
}

If it is 1 it will start loop from begining. Otherwise loop will break. I think that you expected continue to run your code after if else statement but continue runs loop again from the very begining.
If you want to run next line of code after if else just remove continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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