如何最有效地选择3个随机问题并按随机顺序排序?

时间:2014-09-10 11:57:23

标签: java file sorting random

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

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

我目前有2个文件,trivaQ.txt和triviaA.txt。他们都持有"问题1" - "问题10"和#34;答案1" - "答案10"。问题1的答案是答案1,问题2的答案是答案2,依此类推。

我编写了一个可以访问该文件的简单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次。

有更有效的方法吗?我的代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

轻松:

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

拨打随机播放后,您也可以从

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

答案 1 :(得分:0)

让我在不使用Google番石榴等的情况下让它工作的最简单方法是创建另一个整数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并在主文件中检查它。这是一个简单的工作。感谢大家的帮助,直到现在我才想到这样做。

  • 康纳