首页 > 解决方案 > 为什么此方法每次都将第一个答案作为正确答案返回?

问题描述

我正在做一个欧洲国旗测验,newQuestion 方法旨在将所有 4 个按钮的答案选择随机排列,但第一个答案选择总是答案?

问题是案例陈述中的参数吗?是我在开始时定义每个按钮的时候吗?我该如何解决这个问题,以便答案选择在所有四个按钮之间有所不同,而不仅仅是第一个?

任何帮助,将不胜感激。

private void newQuestion(int number) {
    iv_flag.setImageResource(list.get(number - 1).getImage());
    int correct_answer = r.nextInt(3) ;
    int firstButton = number-1;
    int secondButton=number;
    int thirdButton=number;
    int fourthButton=number;
    switch (correct_answer) {

        case 0:
            b_answer1.setText(list.get(firstButton).getName());
            do {
                secondButton = r.nextInt(list.size());
            }
            while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);

            do {
                fourthButton = r.nextInt(list.size());
            }
            while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);

            b_answer2.setText(list.get(secondButton).getName());
            b_answer3.setText(list.get(thirdButton).getName());
            b_answer4.setText(list.get(fourthButton).getName());

            break;

        case 1:
            b_answer2.setText(list.get(secondButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            }
            while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);

            do {
                fourthButton = r.nextInt(list.size());
            }
            while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);

            b_answer1.setText(list.get(firstButton).getName());
            b_answer3.setText(list.get(thirdButton).getName());
            b_answer4.setText(list.get(fourthButton).getName());

            break;
        case 2:
            b_answer3.setText(list.get(thirdButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            }
            while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);

            do {
                fourthButton = r.nextInt(list.size());
            }
            while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);

            b_answer2.setText(list.get(secondButton).getName());
            b_answer1.setText(list.get(firstButton).getName());
            b_answer4.setText(list.get(fourthButton).getName());

            break;
        case 3:
            b_answer4.setText(list.get(fourthButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            }
            while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);

            do {
                fourthButton = r.nextInt(list.size());
            }
            while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);

            b_answer1.setText(list.get(firstButton).getName());
            b_answer2.setText(list.get(secondButton).getName());
            b_answer3.setText(list.get(thirdButton).getName());


            break;
    }

}

标签: javaandroid

解决方案


您必须在代码中更正一件事。当你调用 `r.nextInt(3); 你会得到一个从 0 到 2 的数字。所以在你的 switch 中, case: 3 永远不会被执行。你能指定使用什么号码吗?还有你的逻辑是如何工作的?如果您有一个答案列表并将按钮设置为包含从列表中获取的一个随机答案,然后在设置每个按钮后将其删除,这不是更简单吗?

类似的东西(伪代码)

Declare list of answer
String answer1 = list.get(r.nextInt(list.size() -1))
Button1.setText(answer1);
list.remove(answer1);


ecc...

推荐阅读