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

时间:2015-10-31 16:26:27

标签: java do-while

这是我的方法

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");

1 个答案:

答案 0 :(得分:1)

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

{
  test:   /\.styl$/,
  loader: 'style!css!stylus?resolve url'
}

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

你想做的更像是:

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

虽然这段代码可以简化为一个循环。