【问题标题】:How to most efficiently choose 3 random questions and sort them in a random order?如何最有效地选择 3 个随机问题并按随机顺序排序?
【发布时间】:2014-09-10 11:57:23
【问题描述】:

我正在为学校做一个项目,并被要求创建一个出售愚蠢物品的“商店”。在用户将适当数量的商品添加到他/她的购物车后,他们会选择结帐选项。这样做之后,会根据《星球大战》的知识向用户提示一个琐事问题。

对于课堂上的加分,我需要创建一个文件来存储几个问题和答案,然后检查它们是否正确。

我目前有 2 个文件,一个 trivaQ.txt 和一个 triviaA.txt。他们都持有“Question1”-“Question10”和“Answer1”-“Answer10”。 Question1的答案是Answer1,Question2的答案是Answer2,以此类推。

我编写了一个可以访问文件的简单 ReadFile 类,我正在尝试构建一个方法来选择正确答案(与问题索引相同),然后选择其他 2 个随机答案。

这是当前代码:

        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;

}

此方法目前无法按我的意愿工作。它选择一个问题,然后选择正确答案和 2 个随机答案;它没有按正确的顺序对它们进行排序。

我检查返回的值 x 是否等于用户输入,虽然它可能工作了 6/10 次,但失败了 4/10 次。

有没有更有效的方法来做到这一点?我的代码出了什么问题?

【问题讨论】:

  • 在什么情况下会失败?
  • 如果你想让你的代码更高效,你可能想看看codereview.stackexchange.com
  • @ Dave Newton - 当我运行程序时,它没有正确排序它们,所以 cAnswer 作为第二个选项打印出来,但 x 值为 3。

标签: java file sorting random


【解决方案1】:

简单:

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):

调用 shuffle 后,您也可以从

中读取值
itemData.get(0)
itemData.get(1)
itemData.get(2) 

【讨论】:

  • 你能把相关文档的链接贴出来让我看看吗?我不知道那是什么。
【解决方案2】:

对我来说,无需使用 Google guava 等即可使其工作的最简单方法是创建另一个整数 reAnswer。

        int reAnswer = 0;

之后:

        //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");

我会补充:

        reAnswer = 1;

或者任何对应的正确答案。然后我从方法返回值 reAnswer 并在主文件中检查它。这是一个简单的解决方法。谢谢大家的帮助,直到现在我才想到要这样做。

  • 康纳

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2011-06-18
    • 2011-02-01
    相关资源
    最近更新 更多