2017-01-22 7 views
-2

私は複数選択のクイズゲームを作っており、jsonを使っています。私は各質問のシーケンスをランダム化する必要があります。そして、私は各質問のためのタイマーが必要です。タイマーの作成にはどのようなコードを使用できますか?Androidスタジオでタイマーを入れて各質問をランダム化するにはどうすればいいですか?

QuestionLibrary 

public class QuestionLibrary { 

    private String mQuestions [] = { 
     "Which part of the plant holds it in the soil?", 
     "This part of the plant absorbs energy from the sun.", 
     "This part of the plant attracts bees, butterflies and hummingbirds.", 
     "The ______ holds the plant upright." 


    }; 

    private String mChoices [][] = { 
     {"Petals", "Roots", "Stem", "Flower"}, 
     {"Fruit", "Petals","Leaves", "Seeds"}, 
     {"Bark", "Flower", "Petals","Roots"}, 
     {"Flower", "Leaves", "Stem", "Petals"} 

    }; 

    private String mCorrectAnswers[] = {"Roots", "Leaves", "Flower", "Stem"}; 

    //return a question after a question 
    public String getQuestion(int a) { 
      String question = mQuestions[a]; 
      return question; 
    } 

    public String getChoice1(int a) { 
      String choice0 = mChoices[a][0]; 
      return choice0; 
    } 

    public String getChoice2(int a) { 
      String choice1 = mChoices[a][1]; 
      return choice1; 
    } 

    public String getChoice3(int a) { 
      String choice2 = mChoices[a][2]; 
      return choice2; 
    } 

    public String getChoice4(int a) { 
      String choice3 = mChoices[a][3]; 
      return choice3; 
    } 

    public String getCorrentAnswer(int a){ 
     String answer = mCorrectAnswers[a]; 
     return answer; 
    } 

} 
+1

定期的に質問と選択肢を生成し、リストからランダムな質問を選択するために 'Random'を使用するために、' Timer'または 'CountDownTimer'を使用してください。 [CountDownTimer](https://developer.android.com/reference/android/os/CountDownTimer.html)、 [Timer](https://developer.android.com/reference/java/util/)のリンクを参照してください。 Timer.html)、 [ランダム](https://developer.android.com/reference/java/util/Random.html) – Sony

答えて

0

このように質問、オプション、および回答を1つのクラスに入れるとよいでしょう。

class Question { 
public String question ; 
public String[] options ; 
public String answer ; 
} 

リストに質問を追加質問

List<Question> questionList = new ArrayList<Question>(); 

のリストを作成します:Collections.shuffle()

Collections.shuffle(questionList); 

を使用してランダムな順序で質問を取得するには、リストをシャッフル

Question question1 = new Question(); 
question1.question = "Which part of the plant holds it in the soil?" ; 
question1.options = {"Petals", "Roots", "Stem", "Flower"} ; 
question1.answer = "Roots" ; 

questionList .add(question1); //Similarly add all your questions into the list 

をタイマーのためには、CountDownTimerを使用することができ、以下のリンクをチェックし

Link

関連する問題