我正在进行多项选择测验游戏并使用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;
}
}
答案 0 :(得分:0)
最好将问题,选项和答案放在这样的一个类中。
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);
对于Timer,您可以使用CountDownTimer检查以下链接