简体   繁体   English

数组随机问题

[英]Random questions with arrays

we create a small quiz app for the university in a project. 我们在一个项目中为大学创建了一个小型测验应用。 But we are not programmers. 但是我们不是程序员。 I have now created 17 questions and packed them into 17 arrays. 我现在已经创建了17个问题并将它们打包成17个数组。 The question is always at position 0 of the array. 问题始终在数组的位置0。 The answer is always at position 1 of the array. 答案始终在数组的位置1。 Everything also works correctly. 一切也正常工作。 But I would like to insert a random order. 但我想插入一个随机命令。 I already tried Switch Case or randomshuffle, no success and the answers no longer match the questions. 我已经尝试过Switch Case或randomshuffle,但没有成功,答案不再符合问题。

I would be very happy if someone could help us. 如果有人可以帮助我们,我将非常高兴。 Thank you very much. 非常感谢你。

This is the 17th array. 这是第17个数组。 I think it makes no sense to show them all. 我认为将它们全部显示是没有意义的。

    String[] frage17 = new String[5];
    frage17[0] = "Marke";
    frage17[4] = "Produkt/Dienstleistung";
    frage17[2] = "Preis/Verkaufsbedingungen";
    frage17[3] = "Distribution";
    frage17[1] = "Kommunikation";

Right now this is my manual order. 现在这是我的手动订单。 How can I now create a random order without having to change my arrays? 现在如何在不更改数组的情况下创建随机订单?

    fragen.add(0, frage17);
    fragen.add(1, frage11);
    fragen.add(2, frage13);
    fragen.add(3, frage4);
    fragen.add(4, frage7);
    fragen.add(5, frage1);
    fragen.add(6, frage5);
    fragen.add(7, frage16);
    fragen.add(8, frage9);
    fragen.add(9, frage8);
    fragen.add(10, frage10);
    fragen.add(11, frage2);
    fragen.add(12, frage12);
    fragen.add(13, frage14);
    fragen.add(14, frage15);
    fragen.add(15, frage6);
    fragen.add(16, frage3);

Output at the end: 最后输出:

ArrayList<String[]> getFragen() {
    return fragen;

Ideally, what you need is a Map between the question and answer, so that even if the questions are shuffled, you always get the right answer. 理想情况下,您需要的是问题与答案之间的Map ,这样即使问题被打乱,您也始终可以获得正确的答案。

Map<String,String> map = new HashMap<>();
map.put("How are you?","I am fine"); // similarly for all questions

Then use map.entrySet() to get EntrySet<Key,Value> pair. 然后使用map.entrySet()获得EntrySet<Key,Value>对。 Additionally, you can even use Collections.shuffle(new ArrayList<>(map.entrySet())); 此外,您甚至可以使用Collections.shuffle(new ArrayList<>(map.entrySet())); to shuffle the obtained EntrySet . 随机播放获得的EntrySet Best wishes! 最好的祝愿!

Judging by what you want to accomplish, it would make more sense to create a class as per the requirements for each frage item and then create an array of such items. 根据您要完成的工作来判断,按照每个frage项目的要求创建一个类,然后创建此类物品的数组会更有意义。

From the code you posted, it seems like you are using Java, thus, this is the part of the code to create a class: 从发布的代码看来,您似乎正在使用Java,因此,这是创建类的代码的一部分:

public static class frage   {
    private String data01;
    private String data02;
    private String data03;
    private String data04;
    private String data05;


    public frage(String data01, String data02, String data03, String data04, String data05) {
        this.data01 = data01;
        this.data02 = data02;
        this.data03 = data03;
        this.data04 = data04;
        this.data05 = data05;
    }

    public String getData01()   {
        return data01;
    }

    public String getData02()   {
        return data02;
    }

    public String getData03()   {
        return data03;
    }

    public String getData04()   {
        return data04;
    }

    public String getData05()   {
        return data05;
    }
}

This class is equivalent to the array of 5 strings that you created. 此类等效于您创建的5个字符串的数组。 To create an item of this class, do it like so: 要创建此类的项目,请按以下方式进行操作:

frage frage1 = new frage("Marke", "Produkt/Dienstleistung", "Preis/Verkaufsbedingungen", "Distribution", "Kommunikation");

And similarly, create the rest of your objects. 同样,创建其余的对象。 Modify the name of data01 and other variables (strings) that are declared inside the class as per your convenience. 根据您的方便,修改data01的名称以及在类内声明的其他变量(字符串)。

Since you mentioned that you do not have coding experience, to get the data from an item you can use: 由于您提到您没有编码经验,因此可以使用以下项目来获取数据:

String result = frage1.getData01();

Here, frage1 was the item that I created in the previous code snippet. 在这里, frage1是我在上一个代码片段中创建的项目。 And the data present inside data01 of frage1 is now present inside result , you can use this data however you like. 并且data01frage1内部存在的数据现在也存在于result内部,您可以随意使用该数据。

Now, create a list of items of frage , in the code snippet above, I create frage1 , in the same manner, create other 16 items (or as many as you want)! 现在,创建frage的项目列表,在上面的代码片段中,我以相同的方式创建frage1 ,创建其他16个项目(或frage1多个)!

Create an ArrayList like so: 创建一个ArrayList如下所示:

ArrayList<frage> fragen = new ArrayList<frage>();

And put these items inside the array list created above: 并将这些项目放在上面创建的数组列表中:

fragen.add(frage1);

Similarly, add the rest of the items that you create. 同样,添加您创建的其余项目。 Now, whenever you want to get a random frage item from the arraylist, start by importing java.util.Random and creating an object of Random , ie add this to the very beginning of your program: 现在,每当您想从arraylist中获取随机的frage项目时,首先要导入java.util.Random并创建一个Random对象,即将其添加到程序的最开始:

import java.util.Random;

Random rand = new Random();

Now, whenever in the program you want to get a random item of frage , use this code: 现在,无论何时要在程序中获取随机的frage项,请使用以下代码:

frage random_item = fragen.get(rand.nextInt(fragen.size()));

And so, you will get a random frage item that will contain all the details about your question. 因此,您将获得一个随机的frage项目,其中包含有关您的问题的所有详细信息。 Here, frage was the name of the class that we created, fragen was the name of the ArrayList that we created and rand was the instance of Random that should be created at the beginning. 在这里, frage是我们创建的类的名称, fragen是,我们创造和ArrayList的名称rand是实例Random应在开始创建。

If you want to get data01 from the random_item that you just got, use: 如果要从刚刚获得的random_item获取data01 ,请使用:

String data = random_item.getData01();

And data will now contain the value of data01 . 现在, data将包含data01的值。 Hope this was of some help. 希望这会有所帮助。 If you want to clarify something, post it in the comments and I shall try my best to help ;) 如果您想澄清一些内容,请将其张贴在评论中,我将尽力提供帮助;)

You should create a class that holds each question and answer instead of using an array here. 您应该创建一个包含每个问题和答案的类,而不是在此处使用数组。 The rule of thumb is that arrays should be used for a sequence of similar objects. 经验法则是,应将数组用于一系列相似的对象。 In this case, "question" and "answer" are not similar, so you shouldn't use an array. 在这种情况下,“问题”和“答案”并不相似,因此您不应使用数组。

When you use this approach, shuffling the questions becomes pretty simple. 使用这种方法时,对问题进行混排变得非常简单。 You create an List with instances of your new class then shuffle that list. 您使用新类的实例创建一个List ,然后对该列表进行混洗。

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

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