我试图做一个游戏,你只能持续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秒后结束,我希望它使程序离开while循环返回break语句。 有什么建议?
你的代码更改为这
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! ");
}
一旦运行函数退出,t.isAlive会是假的。 您可能还需要通过周围的定时器对象和检查的IsAlive()的对象,这取决于CheckGame是如何工作的。 这是使得输入不能被放置在10秒后的时间无限期。
下面是一个简单的演示,它可以让你知道如何使用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 :");
}
}
}
}
编辑
由于我没有你的所有代码,所以我在这里发布我的基本假设你的答案的解决方案。 不要使用线程。 而是使用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
文章来源: trying to program a timer so that a user can only input words for a certain amount of time