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

时间:2013-03-31 17:33:57

标签: java timer

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

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循环。有什么建议?

3 个答案:

答案 0 :(得分:1)

将您的代码更改为此

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秒后无限期地输入。

答案 1 :(得分:1)

这是一个简单的演示,让您了解如何使用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 ");
        }
    }
}

答案 2 :(得分:0)

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

计时器可以如下;

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