用户输入在while循环期间;循环/块循环 - Java

时间:2017-09-26 02:45:33

标签: java while-loop java.util.scanner timeunit

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

0 个答案:

没有答案