我正在进行一个问答游戏,我需要随机化每个问题。我该如何正确编码这些。请帮助。感谢
import java.util.ArrayList;
import java.util.List;
public class Question {
public String questions;
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);
Collections.shuffle(questionList);
答案 0 :(得分:-1)
此
Collections.shuffle(questionList);
只调整一个Question对象列表(你只有一个问题......),而不是选项......
您应该使用其他逻辑,例如在 0 ans options.length-1 之间生成随机整数或使用可排序的数据结构