- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码,
import java.util.Scanner;
public class Dummy {
static Scanner sc = new Scanner(System.in);
public static int getIntegerInput(String prompt){
int choice = 0;
for(;;){
System.out.print(prompt);
try{
choice = sc.nextInt();
break;
}catch(java.util.InputMismatchException ex){
System.out.print("What??? ");
}
}
return choice;
}
public static void main(String[] args) {
int choice = getIntegerInput("Enter a number: ");
} //end main
}
如果第一个用户输入引发异常,则不会停止等待下一个用户输入。
如何理解上面代码中的这个问题?放置sc.next()
在catch
解决问题。但我仍然不清楚幕后发生了什么?解决这个问题的正确方法是什么?
最佳答案
当 nextXYZ()
无法使用 token 时,它会将其保留在 InputStream
中。在这里,您无休止地循环相同的输入 - 每次迭代,您都尝试使用此 token ,如果它不是整数,则抛出异常,捕获它,然后尝试再次读取它 - 永远。
编辑:
为了解决这个问题,您可以使用 next()
来使用该 token 并继续处理下一个:
for(;;) {
System.out.print(prompt);
try{
choice = sc.nextInt();
break;
} catch(java.util.InputMismatchException ex) {
sc.next(); // here
}
}
关于java - 为什么使用 nextInt() 的这段代码会永远循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616929/
我正在编写一个 Bukkit 插件,它将雪球视为彩弹。当我不小心使用时: (byte) (random.nextInt() % 16) 而不是 (byte) (random.nextInt(16))
我是编程初学者(学习Java)。我正在尝试编写一个程序,其中列出了四个不同的选项供用户选择。 这是其中的一部分: import java.util.*; public class fight
我正在使用以下代码来学习java套接字编程。它的作用是,client.java 程序从用户那里获取一个号码并将其发送到 sever.java。然后服务器将其乘以2并发回给客户端。在我的客户端程序中,它
这个问题已经有答案了: How to use java.util.Scanner to correctly read user input from System.in and act on it?
我正在使用 random 类生成 1 到 5 之间的随机数,如 myrandom.nextInt(6),它工作正常,但我想知道是否有一种方法可以给特定数字赋予权重以增加它出现的概率,假设我希望数字“4
我正在编写一个程序,该程序使用 nextInt(); 在程序中接受两个整数,并将其包装在 try catch block 中以阻止错误输入,例如 double 或字符。 当输入多个错误输入时,循环会重
我不是 Java 专家,但我有一个问题。我使用 Scanner 从键盘获取数据,但在将数据注册到数组之前我必须进行 2 次验证。一是数据不必是字符串,我使用 !in.hastNextInt() 来验证
每当我尝试执行它时,它都会在我的应用程序上给我 FC int a, b; Random s = new Random(); if (diffi.contains("easy") && name
我正在尝试从以下格式的文本文件中解析信息: WarningGeotask: 0, 1 第一个词是某个对象的关键字,要在其后的数字中给出的坐标位置创建。这是我的循环目前的样子: // Open file
我通过输入 Scanner class 请求电话号码,我只强制输入号码,但我可能会出错。我需要的是一串整数,而不是一个整数并且比一个整数的大小容量更大的东西。最终,它的长度需要是 7 个整数或 10
这个问题在这里已经有了答案: How to use java.util.Scanner to correctly read user input from System.in and act on
我需要读入一个整数,如果它不是整数则显示一条消息,我需要循环直到只输入一个 int。我在下面的代码中遇到的问题是当它循环时它不会等待读取 nextInt,它只会继续循环 - 打印出重试消息。
我有一个使用 SecureRandom 实例并获取下一个随机数的类。 让我们说这个例子是: public class ExampleClass() { public void method()
有谁知道Java的Random.nextInt()一段时间后会重演吗? 具体有没有这样的号码n使得以下两个列表相等? List a = new LinkedList<>(); List b = new
在下面的 Java 代码中 Scanner input = new Scanner(System.in); //Cmd1 int i1,i2;
是ThreadLocalRandom's nextInt() method (即没有任何参数的 nextInt() 方法)实际上生成一个没有范围的伪随机整数(即在 Integer.MIN_VALUE
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 7年前关闭。 Imp
这是我正在为类制作的一个程序。在编写了大部分程序后,我现在尝试运行它,似乎有一个逻辑错误。由于某种原因,计算机只会执行到第一个 for 循环,然后忽略 scan.nextInt 方法,在这些方法中我尝
每次我尝试执行包含 .nextInt() 的代码行时,都会收到此错误:“无法对非静态字段进行静态引用” . 以下是可能影响的代码行(我能想到的): private Scanner input = ne
我问的问题不太清楚,但基本上我的任务是: Write a program to read in 5 integer numbers from the user. Youshould store the
我是一名优秀的程序员,十分优秀!