java 如何限制用户只能输入一个整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12610227/
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
How to limit the user to only input an integer?
提问by billabrian6
First, I would like to say that this is for a homework assignment and I'm only looking for suggestions. Not answers! I am very determined to learn and be good at programming, and that doesn't come from someone else doing your work. Point me in the correct direction and it will be greatly appreciated!
首先,我想说这是一个家庭作业,我只是在寻找建议。不是答案!我非常有决心学习并擅长编程,这不是来自其他人在做你的工作。指出我正确的方向,我将不胜感激!
Please know that I have searched the internet for a solution but haven't found one that fits my needs. I'm unable to use any advanced methods.
请注意,我已经在互联网上搜索了解决方案,但没有找到适合我的需求。我无法使用任何高级方法。
The program is to allow the user to input a start and end number for a range. The start number must be divisible by ten and the end must be divisible by ten and not be the same as the starting number. The user is limited to use only numbers between 0 - 1000 and not be allowed to enter any other characters on the keyboard. So if they hit "a" or "1200" the program should loop back until a valid entry has been entered.
该程序允许用户输入范围的开始和结束编号。起始数必须能被十整除,结束数必须能被十整除,且不能与起始数相同。用户只能使用 0 - 1000 之间的数字,并且不允许在键盘上输入任何其他字符。因此,如果他们点击“a”或“1200”,程序应该循环返回,直到输入有效条目。
Currently I'm stuck on only allowing an integer to be input. The particular part of my code is posted below:
目前我坚持只允许输入一个整数。我的代码的特定部分发布在下面:
while(errorLoop != true){
System.out.println("Enter the Starting Number of the Range (ex. 10,70,100)");
startNum = kb.nextInt();
System.out.println("Enter the Ending Number of the Range (ex. 10,70,100)");
endNum = kb.nextInt();
if(startNum % 10 == 0 && endNum % 10 == 0){
errorLoop = true;
}else{
errorLoop = false;
System.out.println("Start and End of range must be divisible by 10\n");
System.out.println("Please try again (ex. 10,70,100)\n");
}
}
I have only posted the part of the code that pertains to the question. If you must know the point of the program, the range of numbers will be sorted by prime numbers and output to a table looking format where each row ends with a number that is divisible by ten. The non-prime numbers will be printed as a "-".
我只发布了与问题有关的代码部分。如果您必须知道程序的要点,数字范围将按素数排序并输出为表格格式,其中每行以一个可被 10 整除的数字结尾。非质数将打印为“-”。
Ex. 71 - 73 - - - - - 79 | 80 \n and it goes on for however big the range is.
前任。71 - 73 - - - - - 79 | 80 \n 无论范围有多大,它都会继续。
回答by Keppil
I would advise you to use nextLine()
instead of nextInt()
. Then you can first make sure that it is parseable as an Integer (check the Integer JavaDoc page), and then that the number meets your requirements.
我建议你使用nextLine()
而不是nextInt()
. 然后您可以首先确保它可以解析为一个整数(检查整数 JavaDoc 页面),然后该数字满足您的要求。
EDIT
To handle the case where the input isn't numeric, you can go in several directions. I prefer checking the input before the actual parse call with a regex. A String
containing only digits would match "^\\d+$"
(check out this linkfor a great regex tutorial), and there is a handy method in the String API.
编辑
要处理输入不是数字的情况,您可以朝多个方向进行。我更喜欢在使用正则表达式进行实际解析调用之前检查输入。String
只包含数字的A将匹配"^\\d+$"
(查看此链接以获得很棒的正则表达式教程),并且String API 中有一个方便的方法。
回答by MrLore
You could always parse your input with a Regex to ensure they're numbers:
您始终可以使用正则表达式解析您的输入以确保它们是数字:
int number = Integer.parseInt(kb.nextLine().replaceAll(”[^\d]“, “”));
Input:
输入:
1blahblah2moretext3
Produces number:
生产数量:
123
回答by PermGenError
write a method that checks if the entered number is numeric.
编写一个方法来检查输入的数字是否为数字。
boolean method(String num) {
boolean retValue;
try {
// check out the Integer API for the methods related to parse an int
retvalue=true;
}
catch(ParseException ex) {
retvalue=false;
}
return retvalue;
}
回答by akshayhallur
Try using the below exceptions in your program, make sure you use InputMismatchException and import it.
尝试在您的程序中使用以下异常,确保使用 InputMismatchException 并导入它。
try {
System.out.println("Enter the value of a");
int a = scanner.nextInt();
System.out.println("Enter the value of b");
int b = scanner.nextInt();
int c = a+b;
System.out.println("The answer is " +c);
}
catch (InputMismatchException exception)
//Add import java.util.InputMismatchException; at the top
{
System.out.println("Error - Enter a integer");
}