1

Im coding who wants to be a millionaire, i used arrays and then a loop, each corresponding with each other, however what if i want to randomize each question and that answer will have to correspond.

 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 };
user1234556
  • 21
  • 1
  • 5

5 Answers5

1

Use OOP.

public class QuestionAndAnswers {

    private String question;
    private String[] answers;
    private int correctAnswer;
    //getters, setters, constructors
}

then you can have List<QuestionAndAnswers> which you can shuffle using:

Collections.shuffle(myList);

If you want to stick with your three separate arrays, you can do the following:

List<Integer> list = IntStream.range(1, n).boxed().collect(Collectors.toList());
Collections.shuffle(list);

and then

for (Integer i : list) {
    //use random in [0,n)
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
0

You may want to look into creating an object to represent your questions. This way you can create a new question, save the answers and the correct answer with the question. Save them all into an array, then you can randomize the array while keeping all of your questions and answers together, and can access them from the array as well.

0

Use Random in a utility method and you could use a wrapper object that contains both Question and 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();

If you want that a question be not repeated, you could create a List for the questions and remove the question of the List when it was retrieved.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

An expedient approach to solving your problem is to create a permutation array of n slots, fill it with numbers from 0 to n-1, inclusive, and perform random shuffle on it:

List<Integer> perm = new ArrayList<>();
for (int i = 0 ; i != ques.length ; i++) {
    perm.add(i);
}
Collections.shuffle(perm);

Now you can go through perm, and pick questions according to perm[i]. The questions would come in randomized order, without repetitions.

However, this is not the most Java-like solution: your program uses Parallel Arrays, the simplicity of which is more than offset by maintenance headaches that they create.

A better approach is to create a class that includes a question, and a list of its answers, and the index of the correct one. Then you can shuffle the questions, and the rest of the information would shuffle with them:

class Question {
    private String question;
    private String[] answers;
    private int correctAnswer;
}

An advantage of this approach is that your questions never get "out of sync" with your answers.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Why don't you create class instead. First create your Question class, which can contain, for example, question, answers, correctAnswer.

class Question {
    private String question;
    private String[] answers;
    private int correctAnswer;
}

Then having this Question class, you can just have List of Question-s from which you can retrieve random element:

List<Question> questions = <your_questions>;
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(questions.size());
Question question = questions.get(index);
//use the random question. 

Hope this helps.