Java:如何使用扫描仪检查输入是整数,并且在指定范围内
我的代码遇到了麻烦。
它应该做什么:
- 检查扫描仪“ myScanner”是否是整数
- ,如果它是整数,如果它在1到200之间(包括)问题(包括)
问题:
- 我需要在第一个猜测后两次输入带有正确值的整数'T正确。
这是我的意思是的屏幕截图:问题的屏幕截图
private static int inputGuess () {
Scanner myScanner = new Scanner(System.in);
int guess = 0;
if (myScanner.hasNextInt()) {
guess = myScanner.nextInt();
}
if (1 > guess || guess > 200) {
while (!(myScanner.hasNextInt()) || !(1 <= guess && guess <= 200)) {
while (!myScanner.hasNextInt()) {
myScanner.nextLine();
}
guess = myScanner.nextInt();
if (!(guess >= 1 && guess <= 200)) {
myScanner.nextLine();
}
}
}
return guess;
}
我试图应用 @绿巨人的(最高答案)逻辑(至少我认为我确实做到了)(可悲的是对我的项目的要求),并得到了:
private static int inputGuess () {
Scanner myScanner = new Scanner(System.in);
int guess = 0;
while (!myScanner.hasNextInt() || guess < 1 || guess > 200) {
while (!myScanner.hasNextInt()) {
myScanner.nextLine();
}
guess = myScanner.nextInt();
if (guess >= 1 && guess <= 200) {
break;
}
}
return guess;
}
经过一些测试后,仍然没有任何错误! 如果您仍然发现一个错误,如果您与我分享,我会很高兴!
I'm having trouble with my code.
What it should do:
- Check if Scanner "myScanner" is an Integer
- If it is an Integer, if it is between 1 and 200 (both included)
Problem:
- I need to input an Integer with the correct value twice after the the first guess wasn't correct.
Here is a screenshot of exactly what I mean: Screenshot of the problem
private static int inputGuess () {
Scanner myScanner = new Scanner(System.in);
int guess = 0;
if (myScanner.hasNextInt()) {
guess = myScanner.nextInt();
}
if (1 > guess || guess > 200) {
while (!(myScanner.hasNextInt()) || !(1 <= guess && guess <= 200)) {
while (!myScanner.hasNextInt()) {
myScanner.nextLine();
}
guess = myScanner.nextInt();
if (!(guess >= 1 && guess <= 200)) {
myScanner.nextLine();
}
}
}
return guess;
}
I have tried to apply @Hulk's (top answer) logic (at least I think I did) into a single method (is sadly a requirement for my project) and got this:
private static int inputGuess () {
Scanner myScanner = new Scanner(System.in);
int guess = 0;
while (!myScanner.hasNextInt() || guess < 1 || guess > 200) {
while (!myScanner.hasNextInt()) {
myScanner.nextLine();
}
guess = myScanner.nextInt();
if (guess >= 1 && guess <= 200) {
break;
}
}
return guess;
}
After a bit of testing still no error!
If you still find a mistake I would be happy if you shared it with me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将问题分为较小的部分,这会变得更加简单。首先,解决“读取直到我们获得整数”部分并将其放入方法中,以便我们可以重复使用它:
现在,我们只需要添加范围检查:
您基本上使事情变得过于复杂,请重复范围检查3以不同方式的位置,很容易因所有这些否定而迷失。保持您的方法简单,一次只做一件事情!
为了提示您的更新问题:如果您绝对需要将它们置于一种方法中,这就是结果:
请注意,仍然只有一个地方检查了数字输入的检查,而只有一个地方验证了范围。在编程中,几乎没有理由写两次完全相同的东西 - 不要重复自己! - “原理”/指南有时会缩写为干燥。
This gets a lot simpler if you split your problem into smaller parts. First, solve the "read until we got an integer" part and put it into a method so we can reuse it:
Now, we only need to add the range check:
You basically complicated things too much, by repeating the range check in 3 places in different ways, and it's easy to get lost with all these negations. Keep your methods simple, and only do one thing at a time!
To adress your updated question: If you absolutely need to inline these into one method, this is the result:
Note that there is still only one place where there is a check for numeric input, and only one place where the range is validated. In programming, there is rarely a reason to write exactly the same thing twice - don't repeat yourself! - a 'principle'/guideline sometimes abbreviated as DRY.