为什么在我的 do while 循环中没有到达这一行?

Why is this line not being reached in my do while loop?

这是我的方法

private int GetJudges()
{
    do
    {
        System.out.println("Please enter the number of judges. This number must be between 3 and 6");   

        while(!scan.hasNextInt())
        {
            scan.nextLine();
            System.out.println("Please ensure the number you entered is between 3 and 6");
        }
        numJudges = scan.nextInt();

    } while (!(numJudges >=3 && numJudges<=6));

    return numJudges ;
}

当我输入低于或高于 3 和 6 的数字时,将打印此行:

System.out.println("Please enter the number of judges. This number must be between 3 and 6"); 

而不是:

 System.out.println("Please ensure the number you entered is between 3 and 6");

您的内部循环仅在您输入非数字的内容时适用:

while(!scan.hasNextInt())
{
    scan.nextLine();
    System.out.println("Please ensure the number you entered is between 3 and 6");
}

如果您输入的数字小于 3 或大于 6,例如 7。您没有理由进入此循环。


你想做的更像是:

System.out.println("Please enter the number of judges.");  
do
{
    System.out.println("Please ensure the number you entered is between 3 and 6");
    while(!scan.hasNextInt())
    {
        scan.nextLine();
        System.out.println("Please ensure the number you entered is a number");
    }
    numJudges = scan.nextInt();

} while (!(numJudges >=3 && numJudges<=6));

虽然这段代码可以减少到只有一个循环。