如何使用 Scanner 只接受有效的 int 作为输入
我正在努力使一个小程序更加健壮,我需要一些帮助。
Scanner kb = new Scanner(System.in);
int num1;
int num2 = 0;
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
while(num2 < num1) {
System.out.print("Enter number 2: ");
num2 = kb.nextInt();
}
数字 2 必须大于数字 1
此外,我希望程序自动检查并忽略用户是否输入字符而不是数字。因为现在当用户输入例如
r
而不是数字时,程序就会退出。
I'm trying to make a small program more robust and I need some help with that.
Scanner kb = new Scanner(System.in);
int num1;
int num2 = 0;
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
while(num2 < num1) {
System.out.print("Enter number 2: ");
num2 = kb.nextInt();
}
Number 2 has to be greater than number 1
Also I want the program to automatically check and ignore if the user enters a character instead of a number. Because right now when a user enters for example
r
instead of a number the program just exits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
Scanner.hasNextInt ()
:下面是一个片段来说明:
您不必
parseInt
或担心NumberFormatException
。请注意,由于hasNextXXX
方法不会前进到任何输入,因此如果您想跳过“垃圾”,您可能必须调用next()
,如上所示。相关问题
Use
Scanner.hasNextInt()
:Here's a snippet to illustrate:
You don't have to
parseInt
or worry aboutNumberFormatException
. Note that since thehasNextXXX
methods don't advance past any input, you may have to callnext()
if you want to skip past the "garbage", as shown above.Related questions
String
然后尝试
ingInteger.parseInt()
如果你没有捕获
异常,那么它就是一个数字,如果你这样做了,读取一个新的,也许通过将 num2 设置为 Integer.MIN_VALUE 并使用您的示例中的逻辑类型相同。String
and thentry
ingInteger.parseInt()
and if you don'tcatch
an exception then it's a number, if you do, read a new one, maybe by setting num2 to Integer.MIN_VALUE and using the same type of logic in your example.这应该有效:
This should work:
试试这个:
您也可以尝试将字符串解析为
int
,但通常人们会尝试避免抛出异常。我所做的是定义了一个定义数字的正则表达式,
\d
表示数字。+
符号表示必须有一个或多个数字。\d
前面多了一个\
是因为在java中,\
是一个特殊字符,所以要转义。Try this:
You could try to parse the string into an
int
as well, but usually people try to avoid throwing exceptions.What I have done is that I have defined a regular expression that defines a number,
\d
means a numeric digit. The+
sign means that there has to be one or more numeric digits. The extra\
in front of the\d
is because in java, the\
is a special character, so it has to be escaped.我发现 Character.isDigit 完全满足需要,因为输入只是一个符号。
当然,我们没有有关此 kb 对象的任何信息,但以防万一它是 java.util.Scanner 实例,我还建议使用 java.io.InputStreamReader 进行命令行输入。这是一个例子:
I see that Character.isDigit perfectly suits the need, since the input will be just one symbol.
Of course we don't have any info about this kb object but just in case it's a java.util.Scanner instance, I'd also suggest using java.io.InputStreamReader for command line input. Here's an example:
您还可以做的是将下一个标记作为字符串, 将此字符串转换为字符数组 并测试 数组中的每个字符都是一个数字。
如果您不想处理异常,我认为这是正确的。
What you could do is also to take the next token as a String, converts this string to a char array and test that each character in the array is a digit.
I think that's correct, if you don't want to deal with the exceptions.