首页 > 解决方案 > 如何停止等待用户输入?

问题描述

我正在构建一个程序来询问乘法,我想设置一个计时器来强制这个人在给定的时间内给出答案:

  1. 如果此人在计时器结束前回答:进行下一个乘法
  2. 如果计时器结束,停止等待用户输入:进行下一个乘法

目前,案例1可以完成,但2不是,我正在考虑一种return;从方法中获取方法的方法,比如线程之类的,但我不知道如何

所以我面临一个问题,如果 aScanner是打开的,等待输入,如何停止它?我试过把它放在一个线程和interrupt()它或boolean用作标志,但它并没有停止Scanner


class Multiplication extends Calcul {    
    Multiplication() {  super((nb1, nb2) -> nb1 * nb2); }   
    @Override
    public String toString() {  return getNb1() + "*" + getNb2(); }
}


abstract class Calcul {

    private int nb1, nb2;
    private boolean valid;
    private boolean inTime = true;
    private boolean answered = false;
    private BiFunction<Integer, Integer, Integer> function;

    Calcul(BiFunction<Integer, Integer, Integer> f) {
        this.nb1 = new Random().nextInt(11);
        this.nb2 = new Random().nextInt(11);
        this.function = f;
    }

    void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("What much is " + this + " ?");

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!answered) {
                    inTime = false;
                }
            }
        }, 5 * 1000);

        int answer = Integer.parseInt(sc.nextLine());
        if (inTime) {
            checkAnswer(answer);
            timer.cancel();
        }
    }    

    private void checkAnswer(int answer) {
        System.out.println("You said " + answer);
        valid = (function.apply(nb1, nb2) == answer) && inTime;
        answered = true;
    }

    int getNb1() {   return nb1;  }    
    int getNb2() {   return nb2;  }    
    boolean isValid() { return valid; }

     public static void main(String[] args) {
         List<Calcul> l = Arrays.asList(new Multiplication(), new Multiplication(), new Multiplication());
         l.forEach(Calcul::start);
}
}

标签: javatimerjava.util.scannerinterrupt

解决方案


您可以检查System.in.available() > 0是否有要读取的行。只有当这返回 true 时才调用sc.nextLine()实际接收输入。

一个例子:

Scanner sc = new Scanner(System.in);

long sTime = System.currentTimeMillis();
while (System.currentTimeMillis() - sTime < 5000)
{
    if (System.in.available() > 0)
    {
        System.out.println(sc.nextLine());
    }
}

sc.close();

如果有要读取的内容,这将从控制台读取 5 秒钟,然后再次打印出来。注意:实际使用它时,您可能会sleep在循环中抛出一个不拥抱许多系统资源的问题。

请注意,这是一个可行的解决方案:available()往往是一种不可靠的方法,它会进行一些估计并且可能是错误的。我可能不会在时间紧迫的系统等中依赖它。

同样为了进一步扩展,这种方法依赖于控制台以大多数控制台的工作方式工作(换句话说:我所知道的所有控制台):只有当用户输入换行符(例如按回车键)时,该行才实际给出System.in来处理。当只输入一个字符时, Elseavailable()已经返回 true。


推荐阅读