繁体   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