简体   繁体   English

如何最有效地选择3个随机问题并以随机顺序对其进行排序?

[英]How to most efficiently choose 3 random questions and sort them in a random order?

I am working on a project for school, and was asked to create a "store" that sells silly items. 我正在为一个学校项目工作,并被要求创建一个“商店”来出售愚蠢的物品。 After the user adds the appropriate number of items to his/her cart, they select the option to checkout. 用户将适当数量的项目添加到他/她的购物车后,他们选择结帐选项。 After doing so, the user is prompted with a trivia question, based on Star Wars knowledge. 之后,根据《星球大战》知识向用户提示琐事问题。

For bonus points in the class, I need to create a file that stores several questions and answers, and then check if they are correct. 为了获得班上的积分,我需要创建一个文件,其中存储了几个问题和答案,然后检查它们是否正确。

I currently have 2 files, a trivaQ.txt and a triviaA.txt. 我目前有2个文件,一个trivaQ.txt和一个triviaA.txt。 They both hold "Question1"-"Question10" and "Answer1"-"Answer10". 他们都持有“ Question1”-“ Question10”和“ Answer1”-“ Answer10”。 Question1's answer is Answer1, and Question2's answer is Answer2, and so on. Question1的答案为Answer1,Question2的答案为Answer2,依此类推。

I wrote a simple ReadFile class that can access the file, and I am trying to build a method that will select the correct answer (the same as the question index) and then 2 other random ones. 我编写了一个可以访问文件的简单ReadFile类,并且尝试构建一种方法,该方法将选择正确的答案(与问题索引相同),然后选择其他2个随机答案。

Here is the current code: 这是当前代码:

        public int askA(){

    int cAnswer = Question;
    int answer1= (int)(Math.random() *10);
    int answer2= (int)(Math.random() *10);



    while(answer1 == cAnswer || answer1 == answer2){
        answer1 = (int)(Math.random() *10);
    }
    while(answer2 == cAnswer || answer2 == answer1){
        answer2 = (int)(Math.random() *10);

    }


    int x = random.nextInt(3)+1;
    int y = random.nextInt(3)+1;
    int z = random.nextInt(3)+1;


    while( x == y || x == z){
        x = random.nextInt(3)+1;
    }

    while( y == x || y == z){
        y = random.nextInt(3)+1;
    }

    while( z == x || z == y){
        z = random.nextInt(3)+1;
    }


    if(x > y && x > z){
        //x is first
        if(y > z){
            //y is second
            //z is third
            System.out.println("[1.] " + itemData.get(cAnswer));
            System.out.println("[2.] " + itemData.get(answer1));
            System.out.println("[3.] " + itemData.get(answer2));
            System.out.println("Answer is 1");

        }else{
            //z is second
            //y is third
            System.out.println("[1.] " + itemData.get(cAnswer));
            System.out.println("[2.] " + itemData.get(answer2));
            System.out.println("[3.] " + itemData.get(answer1));
            System.out.println("Answer is 1");
        }


    }else if(y > x && y > z){
        //y is first
        if(x > z){
            //x is second
            //z is third
            System.out.println("[1.] " + itemData.get(answer1));
            System.out.println("[2.] " + itemData.get(cAnswer));
            System.out.println("[3.] " + itemData.get(answer2));
            System.out.println("Answer is 2");
        }else{
            //z is second
            //x is third
            System.out.println("[1.] " + itemData.get(answer1));
            System.out.println("[2.] " + itemData.get(answer2));
            System.out.println("[3.] " + itemData.get(cAnswer));
            System.out.println("Answer is 2");
        }


    }else if(z > y && z > x){
        //z is first
        if(y > x){
            //y is second
            //x is third
            System.out.println("[1.] " + itemData.get(answer2));
            System.out.println("[2.] " + itemData.get(answer1));
            System.out.println("[3.] " + itemData.get(cAnswer));
            System.out.println("Answer is 3");
        }else{
            //x is second
            //y is third
            System.out.println("[1.] " + itemData.get(answer2));
            System.out.println("[2.] " + itemData.get(cAnswer));
            System.out.println("[3.] " + itemData.get(answer1));
            System.out.println("Answer is 3");
            }
        }
    System.out.println("X is - " + x);
    System.out.println("CorrectAnswer --- " + cAnswer);


    return x;

}

This method currently doesn't work as I want it to. 该方法目前无法正常运行。 It selects a question and it selects the correct answer and 2 random answers, however; 它选择一个问题,然后选择正确的答案和2个随机答案。 it does not sort them in the proper order. 它没有按照正确的顺序对它们进行排序。

I check if the returned value x is equivalent to the user input, and while it works probably 6/10 times, it fails 4/10 times. 我检查返回的值x是否等于用户输入,虽然它可能工作6/10次,但失败4/10次。

Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点? What is going wrong with my code? 我的代码出了什么问题?

Easy as : 简单如:

java.util.Collections.shuffle(itemData);
result = FluentIterable.from(itemData).limit(3).toList(); //(using Google Guava)
result = itemData.stream().limit(3).collect(Collectors.toList());//(using java 8):

After you call shuffle you can alternatively read values from 调用随机播放后,您也可以从

itemData.get(0)
itemData.get(1)
itemData.get(2) 

The easiest way for me to get it to work without having to use Google guava etc., was by creating another integer reAnswer. 对于我来说,无需使用Google番石榴等就能使其正常工作的最简单方法是创建另一个整数reAnswer。

        int reAnswer = 0;

After every: 每次之后:

        //z is second
        //y is third
        System.out.println("[1.] " + itemData.get(cAnswer));
        System.out.println("[2.] " + itemData.get(answer2));
        System.out.println("[3.] " + itemData.get(answer1));
        System.out.println("Answer is 1");

I would add: 我会补充:

        reAnswer = 1;

Or whatever the correct answer corresponded with. 或与正确答案相对应的任何答案。 I then returned the value reAnswer from the method and checked it in the main file. 然后,我从方法中返回值reAnswer并在主文件中进行了检查。 It was a simple work around. 这是一个简单的解决方法。 Thank you for the help everyone, I just didn't think about doing it this way until now. 谢谢大家的帮助,直到现在我还没有考虑过这样做。

  • Connor 康纳

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

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