【问题标题】:Random method for a quiz随机测验方法
【发布时间】:2015-05-22 04:16:09
【问题描述】:

每个方法都包含一个带有多项选择的问题。当我在 main 中调用该方法时,我需要对其进行洗牌并确保没有重复。

public static void main(String[] args) { 
    question_1();
    question_2();
    question_3();
    question_4();
    //continue to question 15
    question_15();
    }

我尝试过的东西。

int question_A = question_1();
int question_B = question_2();
int question_C = question_3();
int question_D = question_4();
//to question 15
int question_O = question_15();
//then i created an array
int [] question = new int[14];
question[0] = question_A;
question[1] = question_B;
question[2] = question_C;
question[3] = question_D;
//to last array
question[14] = question_O;
//to random it here is the code
Random r = new Random();
for (int counter = 0; counter <10; ++counter){
int swap_Index = r.next Int(15-counter)+counter; //there is an space between      next Int, that because i was getting not properly formatted in the edit box
int temp = question[counter];
question[counter] = question[swap_Index];
question[swap__Index] = temp;
int[] question_To_Ask = new int[10];
for (int count = 0; count<10; ++count){
question_To_Ask[count] = question[count];
}

随机不工作的原因是因为它开始执行程序在

int question_A = question_1();

对于随机,我也尝试了任何方式,例如Math.random。这些都不起作用,是的,请不要使用高级技术来解决这个问题,因为我是初学者。

【问题讨论】:

标签: java arrays random methods


【解决方案1】:

最简单的方法是使用列表:

List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);

【讨论】:

    【解决方案2】:

    看,我不知道你在用所有这些方法做什么,但让我们考虑另一种方法,将每个问题放入数据库中,在你的 java 代码访问你的数据库中使用一个 Collection 像说 hashmap 并基于问题的 id你可以调用你想调用的任何问题,对于洗牌部分,有一个在 java 中称为 shuffle 的预定义函数,你可以用它来洗牌你的问题集合。只是一个建议,试试吧,我认为这是一个更好的方法。

    【讨论】:

      【解决方案3】:

      你可以这样做

      public static void main(String[] args) { 
          // declare the variables first
          int q1 = 1;
          int q2 = 2;
          ...
          int q15 = 15;
          int[] questions = new int[] { q1, q2, q3, q4, ... q15 };
      
          System.out.println("Before Shuffle");
          for (int i : questions) {
              System.out.println(i);
          }
      
          shuffle(questions); // here we do the shuffle
      
          System.out.println("After Shuffle");
          for (int i : questions) {
              System.out.println(i);
          }
      }
      
      public static void shuffle(int[] questions) {
          Random random = new Random();
      
          for (int i = 0; i < questions.length; i++) {
              int newIndex = random.nextInt(questions.length - 1);
              swap(questions, i, newIndex);
          }
      }
      
      private static void swap(int[] questions, int oldIndex, int newIndex) {
          int temp = questions[oldIndex];
          questions[oldIndex] = questions[newIndex];
          questions[newIndex] = temp;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 2015-03-19
        • 2015-11-22
        • 1970-01-01
        相关资源
        最近更新 更多