This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jhoffi
Jhoffi wrote ...

2021/6/25

How can I randomise questions?

Jhoffi Jhoffi

2021/6/25

#
I have a quiz game. Right now it just goes from one question to another, everytime in the same order. How can I randomise this?
public void nextQuestion() {
        questionNum++;
        if(questionNum==questions.size()) {
            end();
        }
        else {
            showQuestion();
        }
    }
Super_Hippo Super_Hippo

2021/6/25

#
Assuming you call that method after a question was answered, you could remove the question out of the ArrayList and then switch to a random remaining one.
questions.remove(questionNum);
if (questions.size() == 0)
{
    end();
}
else
{
    questionNum = Greenfoot.getRandomNumber(questions.size());
    showQuestion();
}
Alternatively, you could shuffle the elements of your array randomly at the beginning of your code.
You need to login to post a reply.