简单问卷阵列计划

时间:2016-03-07 16:09:33

标签: java random

我想创建一个简单的问卷调查程序 - 每个问题将有五个问题和4个答案。运行的所有时间,问题和答案顺序都是随机的。

例如:

第一轮:

  1. 什么是红色水果? 一个。 Apple b.Pineapple C。橙色d。梨

  2. 3面有什么形状? 一个。广场b。圈 C。三角d。八边形

  3. 第二轮:

    1. 3面有什么形状? 一个。八角形b。广场 C。圈子d。三角形
    2. 什么是红色水果? 一个。橙色b。苹果 C。梨d。菠萝
    3. - 编辑 - 这是我根据建议制作的草案代码:

      public static void main(String[] args) {
          Scanner input = new Scanner (System.in);
          Random rand = new Random();
      
          ArrayList<ArrayList<String>> arrList = new ArrayList<ArrayList<String>>();
          ArrayList<String> question = new ArrayList<String>();
          ArrayList<String> choiceShuffle = new ArrayList<String>();
          ArrayList<String> choice1 = new ArrayList<String>();
          ArrayList<String> choice2 = new ArrayList<String>();
          ArrayList<String> choice3 = new ArrayList<String>();
      
          int numQuestion = 3;
          int randomNum = rand.nextInt(numQuestion);
          int score = 0;
          String questionShuffle;
          questionShuffle = question.get(randomNum);
          choiceShuffle = arrList.get(randomNum);
      
          question.add("What is after a?");
          question.add("What is after b?");
          question.add("What is after c?");
      
          choice1.add("a"); choice1.add("b"); choice1.add("c");
          choice2.add("d"); choice2.add("c"); choice2.add("e");
          choice3.add("d"); choice3.add("a"); choice3.add("b");
      
          arrList.add(choice1);
          arrList.add(choice2);
          arrList.add(choice3);
      
          for (int x = 1; x <= question.size(); x++){
              System.out.print(question.get(randomNum));
              Collections.shuffle(choiceShuffle);
              System.out.println(choiceShuffle);
          }
      }
      

      }

      我在哪里错了?而且我想创建一个名为“得分”的变量,无论何时选择正确的答案,它都会启动得分++。打开建议。

2 个答案:

答案 0 :(得分:3)

您可以使用Java中提供的Random类来生成随机数。

Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;

这里max将是您的问题数量,在您的特定情况下,您可以将min设置为0,您可以使用Swing类来设计GUI。大多数IDE(如NetBeans / Eclipse)都可以创建GUI而无需编写显式代码。现在,根据您的随机数,您可以访问question[rand]。您可以arraylists实施您的问题和答案,并以问题[rand]的身份访问您的问题。您可以使用

随机播放相应的答案列表
  

Collections.shuffle(answerList);

编辑:以下是供您参考的示例代码:

  

import java.util.ArrayList; import java.util.Iterator;进口   java.util.Random的; import java.util.Collections;

     

public class JavaApplication1 {

public static void main(String[] args) {
     /**  * Create ArrayList in ArrayList Object  */
ArrayList<ArrayList<String>> quesAns = new ArrayList<ArrayList<String>>();
ArrayList<String> answer = new ArrayList<String>();
ArrayList<String> question = new ArrayList<String>();
question.add("what's your fruit?");
question.add("what's your shape?");
answer.add("apple");
answer.add("banana");
answer.add("mango");
quesAns.add(answer);
ArrayList<String> answer2 = new ArrayList<String>();
answer2.add("circle");
answer2.add("rectangle");
quesAns.add(answer2);
Random rand = new Random();
numQues = 2
int randomNum = rand.nextInt(numQues);
System.out.println (question.get(randomNum));
ArrayList<String> answerListShuffle = new ArrayList<String>();
answerListShuffle = quesAns.get(randomNum);
Collections.shuffle(answerListShuffle);
System.out.println(answerListShuffle);

}
     

}

这只是一个示例实现。你可以组成这些arrayllists并根据元素的数量在循环中添加元素

答案 1 :(得分:0)

使用HashMap会更好。因为使用HashMap,您可以映射您的答案对象&amp;问题对象在一起。我觉得这对你来说很容易。

相关问题