我试图用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) {
}
}
}