java 仅用户输入检查 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12487734/
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
user input check int only
提问by BubbleTree
I am trying to have my user input not crash my program by restricting what the user can input such as:
我试图通过限制用户可以输入的内容来让我的用户输入不会使我的程序崩溃,例如:
- only being an int
- being between 1-30
- 只是一个 int
- 在 1-30 之间
The code that I've written works only up to a certain point. If you enter something thats not an int it will check it and ask you to enter again. Then again if you keep typing anything but an int. I have another while loop if it does type an int, and if it's outside the 1-30 zone then it will ask the user to input again. However after that if the user types another "anything but an int" the program will crash. I've tried to combine both the sc.hasnextint()
and the check for input between 1-30 condition but if i put the sc.nextint()
before the sc.hasnextint()
and the user enters anything but an int, the program crashes. If I put it after the condtion loop, then the userinput will not be declared.
我编写的代码只能在一定程度上起作用。如果您输入的不是 int 的内容,它会检查它并要求您再次输入。如果您继续输入除 int 以外的任何内容,则再次输入。如果它确实输入了一个 int,我还有另一个 while 循环,如果它在 1-30 区域之外,那么它会要求用户再次输入。然而之后,如果用户键入另一个“除 int 以外的任何内容”,程序将崩溃。我尝试将 1sc.hasnextint()
和 30 条件之间的输入和检查结合起来,但是如果我将 放在sc.nextint()
之前sc.hasnextint()
并且用户输入除 int 以外的任何内容,程序将崩溃。如果我把它放在条件循环之后,那么用户输入将不会被声明。
int choose;
System.out.print("type an integer: ");
Scanner sc=new Scanner(System.in);
while (!sc.hasNextInt() ) {
System.out.println("only integers!: ");
sc.next(); // discard
}
choose=sc.nextInt();
while (choose<=0 || choose>30)
{
System.out.print("no, 1-30: ");
choose=sc.nextInt();
}
sc.close();
回答by dasblinkenlight
You need to combine the two loops, so that both checks happen every time the end-user enters something new:
您需要组合这两个循环,以便每次最终用户输入新内容时都会进行两次检查:
for(;;) {
if(!sc.hasNextInt() ) {
System.out.println("only integers!: ");
sc.next(); // discard
continue;
}
choose=sc.nextInt();
if( choose<=0 || choose>30)
{
System.out.print("no, 1-30: ");
continue;
}
break;
}
After the loop exits, choose
is a number between 1
and 30
, inclusive.
循环退出后,choose
是1
和之间的数字30
,包括。
回答by gh.
do:
get number from user
if non integer format is entered{
number = -1;}
while: 1 < number < 30
回答by Wilts C
String choose = "";
System.out.println("Test if input is an integer. Type 'quit' to exit.");
System.out.print("Type an integer: ");
Scanner sc=new Scanner(System.in);
choose = sc.nextLine();
while (!(choose.equalsIgnoreCase("quit"))) {
int d = 0;
try {
d = Integer.parseInt(choose);
if (!(d > 0 && d < 31)) {
System.out.println("Being between 1-30");
} else {
System.out.println("Input is an integer.");
}
} catch (NumberFormatException nfe) {
System.out.println("Enter only int");
}
System.out.print("Type an integer to test again or 'quit' to exit: ");
sc = new Scanner(System.in);
choose = sc.nextLine();
}
sc.close();
System.out.print("Program ends.");
回答by PrivateName
See dasblinkenlight's awnser with the NumberFormatException catch. I was thinking of doing that. This also works too:
使用 NumberFormatException 捕获查看 dasblinkenlight 的 awnser。我正在考虑这样做。这也有效:
You need to combine the two loops like so:
您需要像这样组合两个循环:
while(true) {
if(!sc.hasNextInt) {
System.out.println("Only Integers!");
continue;
}
choose = sc.nextInt();
if(choose <= 0) {
System.out.println("The number you entered was too small.");
continue;
} else if(choose > 30) {
System.out.println("The number you entered was too large.\nMax: 30");
continue;
}
break;
}
sc.close();