2011-10-26 23 views
0

私はクイズで質問に答えなければならない時間制限を実装しようとしています。私はタイマーでかなりの部分を発見しましたが、私はそれをすべて一緒にまとめていく方法を知らない。Java:クイズ付きタイマーの使用

質問に15秒間答えてもらいたいです。彼らが時間内に答えると、答えが正しいかどうかをチェックし、次の質問に進むかどうかを尋ねます。

ユーザーが15秒以内に応答しない場合は、回答が間違っていると言い、次の質問に移動するオプションを与えます。

これまで私がこれまで持っていたことは次のとおりです。

for(int i=0; i<quiz.getQuizQuestions().size(); i++){ 

     super.getQuestion(i); 

     //While timer is less than 15 seconds 

     getResponse(i, questionStart); 

     //If time has run out output "You have run out of time" 

     super.nextQuestion(); 


} 

それはおそらく知っている価値がある:キーボード入力を待っている)(

super.getQuestion(I)だけ

てgetResponseを頼まれて質問を印刷しています。何かが入力された場合、ユーザーが正しいかどうかを確認します。

super.nextQuestion()ユーザーは、彼らが次の質問に事前

EDITで

感謝を移動したいです尋ねる:カウントダウンカウンタを実装することは容易であった場合、それはまた素晴らしいだろうこれをGUIに変換するときは15です。

+0

宿題は何時でもですか?もしあれば大丈夫です。 –

答えて

1

は、ExecutorServiceとFutureを使用して、行を読み込んだり、割り込みしたりしていることを確認します。コードは私が予想していたよりも少し時間がかかります...不明な点がある場合はお知らせください:

import java.util.concurrent.*; 
import java.io.*;  

public class Test { 
    public static void main(String[] args) throws java.io.IOException { 
     Question q = new Question(); 
     System.out.println("You have 5 seconds: " + q.toString()); 

     String userAnswer = null;  
     ExecutorService ex = Executors.newSingleThreadExecutor(); 
     try { 
      Future<String> result = ex.submit(new GetInputLineCallable()); 
      try { 
      userAnswer = result.get(5, TimeUnit.SECONDS); 
      if (Integer.valueOf(userAnswer) == q.getAnswer()){ 
       System.out.println("good!"); 
      } 
      else{ 
       System.out.println("Incorrect!"); 
      } 

      } catch (ExecutionException e) { 
      e.getCause().printStackTrace(); 
      } catch (TimeoutException e){ 
      System.out.println("too late!"); 
      return; 
      } catch (InterruptedException e){ 
      System.out.println("interrupted?"); 
      e.getCause().printStackTrace(); 
      } 

     } finally { 
      ex.shutdownNow(); 
     } 
    } 
} 



class GetInputLineCallable implements Callable<String> { 
    public String call() throws IOException { 
    BufferedReader inp = new BufferedReader(new InputStreamReader(System.in)); 
    String input = ""; 
    while ("".equals(input)) { 
     try { 
     while (!inp.ready()) { 
      Thread.sleep(100); 
     } 
     input = inp.readLine(); 
     } catch (InterruptedException e) { 
     return null; 
     } 
    } 
    return input; 
    } 
} 




class Question{ 
    int p1, p2; 
    public Question(){ 
    p1 = 2; 
    p2 = 3; 
    } 
    public String toString(){ 
    return String.format("%d + %d = ?", p1, p2); 
    } 
    public int getAnswer(){ 
    return p1+p2; 
    } 
} 
0
long startTime = System.currentTimeMillis(); 

    while(System.currentTimeMillis() - startTime < 15000){ 
     if(userAnswered){ 
      break; // if there is an answer we stop waiting 
     } 
     Thread.sleep(1000); // otherwise we wait 1 sec before checking again 
    } 

    if(userAnswered){ 
     goToNextQuestion(); 
    } 
    else { 
     handleTimeOut(); 
    } 
+0

そのブロックは? – Serhiy

+0

OPは別のスレッドでこれを使用する必要があります。そうしないと、タイムアウトするまでプログラムがスリープ状態になっているため、ユーザーは応答を入力できなくなります。 –

関連する問題