簡體   English   中英

如何使用 Android Studio 放置計時器並隨機化每個問題?

[英]How to put a timer and randomize each of the questions using Android Studio?

我正在制作一個多項選擇測驗游戲,並且正在使用 json。 我需要隨機化每個問題的順序。 我每個問題都需要一個計時器。 我可以使用什么代碼來制作計時器?

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;
    }

}

最好將問題、選項和答案放在這樣的一類中。

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

創建問題列表

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

將問題添加到列表中:

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

使用Collections.shuffle()隨機排列列表以隨機獲取問題

Collections.shuffle(questionList);

對於計時器,您可以使用 CountDownTimer 檢查以下鏈接

關聯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM