简体   繁体   English

简单问卷阵列程序

[英]Simple Questionnaire Array Program

I want to create a simple questionnaire program - there will be five questions and 4 answers each. 我想创建一个简单的问卷调查程序-每个问题有5个问题和4个答案。 Everything time it is run the questions and answers order will be random. 每次运行时,问题和答案的顺序都是随机的。

Ex: 例如:

1st run: 第一次运行:

  1. What is fruit colored red? 什么是红色的水果? a. 一种。 Apple b.Pineapple c. 苹果b。菠萝c。 Orange d. 橙d。 Pear

  2. What shape has 3 sides? 3边的形状是什么? a. 一种。 Square b. 广场b。 Circle c. 圈c。 Triangle d. 三角形d。 octagon 八边形

2nd run: 第二次运行:

  1. What shape has 3 sides? 3边的形状是什么? a. 一种。 Octagon b. 八角形b。 Square c. 广场c。 Circle d. 圈d。 Triangle 三角形
  2. What is fruit colored red? 什么是红色的水果? a. 一种。 Orange b. 橙b。 Apple c. 苹果c。 Pear d. 梨d。 Pineapple 菠萝

--EDIT-- Here is the draft code I have made based on the suggestions: -编辑-这是我根据建议编写的代码草案:

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

} }

Where am I going wrong here? 我在哪里错了? and also I would like to create a variable named "score" that whenever the right answer is chosen, it will initiate score++. 我也想创建一个名为“分数”的变量,只要选择正确的答案,它将启动score ++。 Open for suggestions. 公开征求意见。

you can use the Random class available in Java for random number generation. 您可以使用Java中可用的Random类来生成随机数。

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

here max will be your number of questions and in your particular case you can set min to 0, you can use Swing classes to design your GUI. 这里max是您的问题数量,在特定情况下,您可以将min设置为0,可以使用Swing类来设计GUI。 Most IDEs like NetBeans/Eclipse have provisions to create GUI without writing explicit code. 像NetBeans / Eclipse这样的大多数IDE都有创建GUI规定,而无需编写明确的代码。 Now depending on your random number you can access the question[rand] . 现在,根据您的随机数,您可以访问question[rand] You can implement your questions and answers as arraylists and access your questions as question[rand]. 您可以将问题和答案实现为arraylists并以问题[rand]访问您的问题。 You can shuffle your corresponding answers list using 您可以使用

Collections.shuffle(answerList); Collections.shuffle(answerList);

Edit: Here's a sample code for your reference: 编辑:这是示例代码供您参考:

import java.util.ArrayList; 导入java.util.ArrayList; import java.util.Iterator; 导入java.util.Iterator; import java.util.Random; 导入java.util.Random; import java.util.Collections; 导入java.util.Collections;

public class JavaApplication1 { 公共类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); } 

} }

This is just an example implementation. 这只是一个示例实现。 You can make up these arrayllists and add elements as you go inside your loops depending on the number of elements 您可以组成这些arrayllist并在循环中添加元素,具体取决于元素的数量

It's better to use HashMap. 最好使用HashMap。 because with HashMap you can map your answer object & question object together. 因为使用HashMap可以将答案对象和问题对象映射在一起。 I think it's easy for you. 我认为这对您来说很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM