简体   繁体   中英

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

I am making a multiple choice quiz game and am using json. I need to randomize the sequence for each of the question. And I need a timer for each question. What code can I use for making a timer?

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

}

Better to put the question,options and the answer into one class like this.

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

Create a list of question

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

Add Question to the list :

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

Shuffle the list to get questions in random order using Collections.shuffle()

Collections.shuffle(questionList);

For Timer you can use the CountDownTimer check the below link

Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM