azr*_*zro 5 java timer interrupt java.util.scanner
我正在构建一个程序来询问乘法,我想设置一个计时器来强制该人在给定时间内给出答案:
目前,案例1可以完成,但2不是,我正在考虑return;从方法内部的方式,如线程或其他东西,我不知道如何
所以我面临一个问题,如果一个Scanner是开放的,等待输入,如何阻止它?我已经尝试将它放在一个线程和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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以检查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();
Run Code Online (Sandbox Code Playgroud)
如果有内容需要读取,它会从控制台读取 5 秒钟,然后再次打印出来。注意:实际使用时,您可能会sleep在循环中抛出一个,以免占用许多系统资源。
请注意,这是一个可行的解决方案:available()往往是一种不可靠的方法,会进行一些估计并且可能会出错。我可能不会在时间紧迫的系统等中依赖它。
另外,为了进一步扩展,这种方法依赖于控制台以大多数控制台的工作方式工作(换句话说:我所知道的所有控制台):仅当用户输入换行符(例如按 Enter 键)时,该行实际上被赋予System.in来处理。available()当只输入一个字符时,Else就已经返回 true。
| 归档时间: |
|
| 查看次数: |
158 次 |
| 最近记录: |