我希望成为百万富翁的编码,我使用数组然后循环,每个都相互对应,但是如果我想随机化每个问题并且答案将必须对应。
String[] ques =
{
"Which of these dance names is used to describe a fashionable dot?",
"What is the only position on a football team that can be 'sacked'?",
"What god of love is often depicted as a chubby winged infant with a bow and arrow?",
"Which of the following months has no U.S. ferderal holiday?",
"What mythological beast is reborn from its own ashes?",
"Who developed the first effective vaccine against polio?",
"Which of the following is not a monotheistic religion?",
"In 2014, 17-year-old Pakistani Malla Yousafzai became the youngest person ever awarded the Nobel Peace Prize in recognition of her work for what?",
"Translated from the Latin, what is the motto of the United States?",
"As part of its maintenance, which of these tourist attractions requires the use of embalming fluid?",
"Gerontology is the study of what?",
"The card game solitaire is also called what?",
"Which of the following is the title of an acclaimed PBS science series?",
"When asked why he wanted to climb Mount Everest, what explorer said, 'Because it's there'?",
"According to the well-known phrase, if a plan is not perfect what will be found 'in the ointment'?" };
//Create answers with 2d array
String[][] answ = { { "1.Hora", "2.Swing", "3.Lambada", "4.Polka" },
{ "1.Center", "2.Wide receiver", "3.Tight end", "4.Quarterback" },
{ "1.Zeus", "2.Mercury", "3.Cupid", "4.Poseidon" },
{ "1.August", "2.February", "3.September", "4.November" },
{ "1.Phoenix", "2.Minotaur", "3.Dragon", "4.Golem" },
{ "1.Albert Sabin", "2.Niels Bohr", "3.Louis Pasteur",
"4.Jonas Salk" },
{ "1.Islam", "2.Judaism", "3.Hinduism", "4.Christianity" },
{ "1.Animal welfare", "2.Freedom of the press",
"3.Nuclear disarmament", "4.Education rights" },
{ "1.In God we trust", "2.One out of many", "3.All as one",
"4.Striving together" },
{ "1.Lenin's tomb", "2.Mount Rushmore", "3.Stonehenge",
"4.Hoover Dam" },
{ "1.Music history", "2.Aging", "3.Color", "4.Grammar" },
{ "1.Patience", "2.Rochambo", "3.Concentration",
"4.Associations" },
{ "1.Nova", "2.Pulsar", "3.Universe", "4.Life" },
{ "1.Reinhold Messner", "2.Sir Edmund Hillary",
"3.Peter Habeler", "4.George Mallory" },
{ "1.Salmon", "2.Frog", "3.Fly", "4.Wildebeest" } };
//Correct answers
int[] correctAnswers = { 4, 4, 3, 1, 1, 4, 3, 4, 2, 1, 2, 1, 1, 4, 3 };
答案 0 :(得分:1)
使用OOP。
public class QuestionAndAnswers {
private String question;
private String[] answers;
private int correctAnswer;
//getters, setters, constructors
}
然后您可以使用List<QuestionAndAnswers>
进行随机播放:
Collections.shuffle(myList);
如果您想坚持使用三个独立的阵列,可以执行以下操作:
List<Integer> list = IntStream.range(1, n).boxed().collect(Collectors.toList());
Collections.shuffle(list);
然后
for (Integer i : list) {
//use random in [0,n)
}
答案 1 :(得分:0)
您可能希望研究创建一个对象来表示您的问题。这样您就可以创建一个新问题,保存答案以及问题的正确答案。将它们全部保存到数组中,然后您可以将数组随机化,同时将所有问题和答案保存在一起,并且也可以从数组中访问它们。
答案 2 :(得分:0)
在实用程序方法中使用Random,您可以使用包含Question
和Answers
的包装器对象:
public QuestionAndAnswers getRandomQuestionAndAnswers(){
Random random = new Random();
int index = random.nextInt(ques.length);
String question = ques[index];
String[] answer = answ[index];
return new QuestionAndAnswers(question, answer);
}
// then use the index to get the question and the answer
QuestionAndAnswers questionAndAnswers = getRandomQuestionAndAnswers();
如果您希望不重复问题,可以为问题创建一个列表,并在检索到列表时删除该问题。
答案 3 :(得分:0)
解决问题的一种便捷方法是创建permutation array个n
个广告位,用0到n-1(包括0和n-1)的数字填充,并在其上执行random shuffle:
List<Integer> perm = new ArrayList<>();
for (int i = 0 ; i != ques.length ; i++) {
perm.add(i);
}
Collections.shuffle(perm);
现在,您可以浏览perm
,然后根据perm[i]
选择问题。问题将按随机顺序排列,不会重复。
但是,这不是最类似Java的解决方案:您的程序使用Parallel Arrays,其简单性远远超过它们创建的维护难题。
更好的方法是创建一个包含问题的类,其答案列表以及正确答案的索引。然后你可以改变问题,其余的信息会随之改变:
class Question {
private String question;
private String[] answers;
private int correctAnswer;
}
这种方法的一个优点是您的问题永远不会与您的答案“不同步”。
答案 4 :(得分:0)
为什么不改为创建类。首先创建您的Question
类,其中可以包含question
,answers
,correctAnswer
。
class Question {
private String question;
private String[] answers;
private int correctAnswer;
}
然后拥有这个Question
课程,你可以List
个Question
- 你可以从中检索随机元素:
List<Question> questions = <your_questions>;
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(questions.size());
Question question = questions.get(index);
//use the random question.
希望这有帮助。