试图对计时器进行编程,以便用户只能在一定时间内输入单词

我正在尝试制作一个只能输入10秒钟的单词的游戏。 我试图创建一个multithreading解决方案,但它无法正常工作。

class timer extends Thread{//thread public void run(){ for(int i=10;i>=0;i--){ System.out.print(i+" "); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 

主要方法:

  timer t=new timer(); t.start(); while () {//not sure what to put in my while statement System.out.print("Guess a word on the board! "); if(test.CheckGame(scan.next())==true){ System.out.print("Good job! "); } else System.out.print("Guess again! "); } 

基本上,在线程持续10秒后终止,我希望它返回一个break语句,以便程序离开while循环。 有什么建议么?

将您的代码更改为此

 timer t=new timer(); t.start(); while (t.isAlive()) {//not sure what to put in my while statement System.out.print("Guess a word on the board! "); if(test.CheckGame(scan.next())==true){ System.out.print("Good job! "); } else System.out.print("Guess again! "); } 

一旦run函数退出,t.isAlive将为false。 您可能还需要传递计时器对象并检查对象的isAlive(),具体取决于CheckGame的工作方式。 这使得输入无法在10秒后无限期地输入。

这是一个简单的Demo,可以让你知道如何使用java.util.Timer

 import java.util.Timer; import java.util.TimerTask; import java.util.Scanner; class Tester { static long i = 0; public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("You have only 10 seconds to find the result"); System.out.println("What is the value of : 111111 X 111111 "); Timer timer = new Timer("Timer"); timer.schedule(new TimerTask() { public void run() { if (i == 12345654321L) { System.out.println("Congrats!! you guessed the write answer :)"); } else { System.out.println("Sorry Time is over. You couldn't guess the correct answer."); } System.exit(0); } },10 * 1000 , 1); while (true) { i = scanner.nextLong(); if ( i == 12345654321L) { System.out.println("Congrats!! you guessed the write answer :)"); System.exit(0); } else { System.out.println("Try next guess :"); } } } } 

编辑

由于我没有您的所有代码所以我在这里发布了基于我的基本假设的答案。 不要使用Thread。 而是使用java.util.Timer 。 您的代码如下所示:

 static String input=" ";//created a static variable input to take input public static void main(String st[]) { Timer timer = new Timer("Timer"); timer.schedule(new TimerTask() { public void run() { if (test.CheckGame(input)) { System.out.println("Congrats!! you guessed the write answer :)"); } else { System.out.println("Sorry Time is over. You couldn't guess the correct answer."); } System.exit(0); } },10 * 1000 , 1);//waits for 10 seconds while (true) { System.out.print("Guess a word on the board! "); input = scan.next(); if(test.CheckGame(input)) { System.out.print("Good job! "); System.exit(0); } else { System.out.println("Bad Guess. Try again "); } } } 

你可以有一个共享的布尔值,你的线程和主要以同步的方式共享。

计时器可以如下;

 class timer extends Thread{//thread private Object lock = new Object(); // a lock used by both your thread and main to access stop boolean private boolean stop = false; public void setStop() // your thread calls this method in order to set stop { synchronized(lock) { stop = true; } } public boolean getStop() // main calls this to see if thread told main to stop. { boolean _stop; synchronized(lock) { _stop = stop; } return _stop; } public void run(){ for(int i=10;i>=0;i--){ System.out.print(i+" "); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setStop(); } } } 

你的主要可以如下:

 timer t=new timer(); t.start(); while (!t.getStop()) {// calls getStop to see if other thread told main to stop System.out.print("Guess a word on the board! "); if(test.CheckGame(scan.next())==true){ System.out.print("Good job! "); } else System.out.print("Guess again! "); } t.join(); // to make sure main terminates after the termination of other thread