![](/img/trans.png)
[英]I want to set a timer in this program so that it will break out of the loop if a certain period of time has passed
[英]trying to program a timer so that a user can only input words for a certain amount of time
我正在嘗試制作一個只能輸入單詞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循環。 有什么建議么?
將您的代碼更改為此
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將為false。 您可能還需要傳遞計時器對象,並檢查對象的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.