1

我正在尝试制作一个只能输入 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 循环。有什么建议么?

4

3 回答 3

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

于 2013-03-31T17:45:44.973 回答
1

Here is a simple Demo that would let you to know how to use 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 :");
            }
        }
    }
}

EDIT

Since I don't have your all code so I am posting here the solution for your answer on my basic assumption. Don't use Thread. Instead use java.util.Timer. Your code would look as follows:

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 ");
        }
    }
}
于 2013-03-31T18:06:30.810 回答
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
于 2013-03-31T17:39:59.440 回答