我正在尝试在 java 中停止带有用户输入的 while 循环。我正在使用扫描仪实用程序。但是,扫描仪会阻止循环继续进行。如何使代码随时启用用户输入而不会阻塞循环?
代码
public static void stopwatch() {
System.out.println("When you are ready, please enter '2'.");
Scanner reply = new Scanner(System.in);
String two = reply.nextLine();
if (two.matches("2")) {
double x = 0;
int three = reply.nextInt();
while (three != 3) {
try {
TimeUnit.MILLISECONDS.sleep(1);
x = x+.001;
System.out.println(x);
three = reply.nextInt();
} catch (InterruptedException e) {
}
}
}