java 使用while循环java检查来自扫描仪的输入是否为int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17985575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:45:38  来源:igfitidea点击:

checking if input from scanner is int with while loop java

javawhile-loop

提问by Mo2

I basically want the following while loop to check if the input is an integer. It cannot contain decimals because it is pointing to an array. If the value entered is a decimal it should prompt the user again. Problem is I get two prompts before the while loop starts with this code. Any ideas?

我基本上想要以下 while 循环来检查输入是否为整数。它不能包含小数,因为它指向一个数组。如果输入的值是小数,它应该再次提示用户。问题是在使用此代码开始 while 循环之前,我收到了两个提示。有任何想法吗?

      System.out.print("Enter month (valid values are from 1 to 12): ");
    Scanner monthScan = new Scanner(System.in);
    int monthInput = monthScan.nextInt();
    // If the month input is below 1 or greater than 12, prompt for another value
    while(monthInput<1 || monthInput>12 || !monthScan.hasNextInt())
    {
        System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");
        monthInput = new Scanner(System.in).nextInt();
    }

thanks

谢谢

EDIT: The current output gives the following:

编辑:当前输出给出以下内容:

Enter month (valid values are from 1 to 12): 2
2

Notice how I had to enter 2 twice even though it is a valid value.

注意我是如何输入 2 两次的,即使它是一个有效值。

回答by Niklas

Check that the input is integer with hasNextInt()before you call nextInt(). Otherwise nextInt()throws an InputMismatchExceptionwhen user types a non integer.

hasNextInt()在调用之前检查输入是否为整数nextInt()。否则当用户输入非整数时nextInt()抛出一个InputMismatchException

int monthInput;

System.out.print("Enter month (valid values are from 1 to 12): ");
Scanner monthScan = new Scanner(System.in);

if (monthScan.hasNextInt()) {
    monthInput = monthScan.nextInt();
} else {
    monthScan.next();   // get the inputted non integer from scanner
    monthInput = 0;
}

// If the month input is below 1 or greater than 12, prompt for another value
while (monthInput < 1 || monthInput > 12) {
    System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");
    if (monthScan.hasNextInt()) {
        monthInput = monthScan.nextInt();
     } else {
       String dummy = monthScan.next();
       monthInput = 0;
    }
}

回答by Prabhaker A

you can check like this

你可以这样检查

System.out.print("Enter month (valid values are from 1 to 12): ");
Scanner monthScan = new Scanner(System.in);

while(monthScan.hasNext())
{
   if(!monthScan.hasNextInt() && (monthInput<1 ||  monthInput>12))
   {
       System.out.print("Invalid value! Enter month (valid values are from 1 to 12):"); 
       continue;
   }

   // If the month input is below 1 or greater than 12, prompt for another value
  int monthInput = monthScan.nextInt();
  //do your logic here   
  break;//use the break after logic 

}

update
Use breakafter your logic so that It will exit after valid input.

在您的逻辑之后更新
使用break,以便它在有效输入后退出。

回答by Srini

A small modification to your program solves the problem

对您的程序稍作修改即可解决问题

 System.out.print("Enter month (valid values are from 1 to 12): ");
        Scanner monthScan = new Scanner(System.in);
       int monthInput = monthScan.nextInt();
        // If the month input is below 1 or greater than 12, prompt for another value
        while((monthInput<1 || monthInput>12) )
        {
            System.out.print("Invalid value! Enter month (valid values are from 1 to 12): ");

            monthInput = monthScan.nextInt();
        }
        System.out.println("I am here");

Output:

输出:

Enter month (valid values are from 1 to 12): -5
Invalid value! Enter month (valid values are from 1 to 12): -5
Invalid value! Enter month (valid values are from 1 to 12): -2
Invalid value! Enter month (valid values are from 1 to 12): 5
I am here

Hope this helps you.

希望这对你有帮助。