【问题标题】:How to randomize questions and answers for a game? [duplicate]如何随机化游戏的问题和答案? [复制]
【发布时间】:2017-06-02 00:15:34
【问题描述】:

我正在编码谁想成为百万富翁,我使用了数组,然后是一个循环,每个都相互对应,但是如果我想随机化每个问题并且答案必须对应怎么办。

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

【问题讨论】:

  • 有人可以给我一个使用我创建的数组的例子吗?

标签: java loops random


【解决方案1】:

您可能想研究创建一个对象来表示您的问题。这样您就可以创建一个新问题,保存问题的答案和正确答案。将它们全部保存到一个数组中,然后您可以将数组随机化,同时将所有问题和答案放在一起,并且也可以从数组中访问它们。

【讨论】:

    【解决方案2】:

    在实用方法中使用 Random,您可以使用包含 QuestionAnswers 的包装器对象:

    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】:

      解决您的问题的一种权宜方法是创建一个permutation arrayn 插槽,用从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】:

        你为什么不创建类呢。首先创建您的Question 类,它可以包含例如questionanswerscorrectAnswer

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

        然后有了这个Question 类,你可以只拥有Question-s 中的List,你可以从中检索随机元素:

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

        希望这会有所帮助。

        【讨论】:

          【解决方案5】:

          使用 OOP。

          public class QuestionAndAnswers {
          
              private String question;
              private String[] answers;
              private int correctAnswer;
              //getters, setters, constructors
          }
          

          然后你可以拥有List&lt;QuestionAndAnswers&gt;,你可以使用它来洗牌:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-06
            • 1970-01-01
            相关资源
            最近更新 更多