简体   繁体   English

为什么数字2注释之后的语句被标记为“无法访问”?

[英]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. 在下面的代码中,数字2之后的语句标记为不可访问。 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. 因为在上一个if ,将执行以continue结尾的分支或以break结尾的分支。

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. 该语句中的第一个选项导致重新启动循环,第二个选项-结束循环并在while循环之后开始执行代码。 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. 您正在使用continuebreak错误。 Your break s should be continue s and your continue s should be simply removed. 您的break应该是“ continue而您的“ continue应该被简单地删除。

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. break退出while(true)循环,由于该循环之后没有其他内容,因此程序结束。 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 . 相反,您想在此时开始一个新的循环迭代(并跳过当前迭代的其余部分),因此您需要continue

As mentioned earlier, continue starts a new loop iteration. 如前所述, continue开始新的循环迭代。 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. 您不需要花哨的东西:只需让if块执行其操作并在if...else语句之后继续。

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. 我建议您考虑对pi的数字进行 for循环以便可以将“当前数字”与输入的数字进行比较。

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. 您的第一个if语句失败或继续循环(它不会继续执行“代码”),因此第一个if语句之后将不会执行任何操作。

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. 您可能需要考虑对请求进行不同的编码(例如,在字符串答案中逐步浏览pi的数字,从而向用户提供反馈)。

All code below //Digit 2 comment is unreachable. //Digit 2注释下的所有代码均无法访问。 I think that you didn't get what continue statement means. 我认为您没有得到continue声明的意思。 When you run while loop it gets int from keyboard by 当您运行while循环时,它会从键盘获取int

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. 如果为1 ,则将从头开始循环。 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 else语句之后continue运行您的代码,但是continue run从一开始就再次循环。
If you want to run next line of code after if else just remove continue. 如果要在之后运行下一行代码, if else删除继续。

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

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