如何在没有重复的情况下从arraylist中选择4个不同的文本

时间:2016-07-06 20:06:52

标签: java android

该应用程序运行正常,但按钮上的答案有时会重复。

如何防止这种情况发生?

到目前为止,这是尝试过的。

public class RiddleGuessActivity extends AppCompatActivity {
    TextView riddleQuestionTextView;
    Button btn1, btn2, btn3, btn4;

    ArrayList<String> riddles = new ArrayList<>();
    ArrayList<String> answers = new ArrayList<>();
    int locationOfCorrectAnswers = 0,incorrectAnswers;
    int riddleCounter = 0;
    ArrayList<String> options = new ArrayList<>();

    public void generateRiddles () {

        riddles.add("I kiss my mother before i die. What am i ?");
        riddles.add("It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it you'll die. What am i ?");
        riddles.add("It walks on four legs in the morning, two legs at noon and three legs in the evening. What am i ?");
        riddles.add("I am the beginning of the end, and the end of time and space. I am essential to creation and i surround everyplace. What am i ?");
        riddles.add("What always runs but never walks, often murmurs, never talks, has a bed but never sleeps, has a mouth but never eats. What am i ?");
        riddles.add("At night they come without being fetched, By day they are lost without being stolen. What am i ?");
        riddles.add("The one who makes it, sells it. The one who buys it, never uses it. The one that uses it never knows that he's using it. What am i ?");
        riddles.add("The more you have of it, the less you see. What am i ?");
        riddles.add("I am always hungry, i must be fed, the finger i touch will soon turn red. What am i ?");
        riddles.add("If you break me, i do not stop working, if you touch me, i may snared, if you lose me, nothing will matter. What am i ?");

        answers.add("Matches");
        answers.add("Nothing");
        answers.add("Man");
        answers.add("Letter E");
        answers.add("River");
        answers.add("The Stars");
        answers.add("A Coffin");
        answers.add("Darkness");
        answers.add("Fire");
        answers.add("Heart");

        Random random = new Random();

        riddleQuestionTextView.setText(riddles.get(riddleCounter));

        locationOfCorrectAnswers = random.nextInt(4);

        options.clear();

        for (int i = 0; i < 4; i++) {
            if (i == locationOfCorrectAnswers) {
                options.add(answers.get(riddleCounter));
            }else {
                incorrectAnswers = random.nextInt(riddles.size());
                while (incorrectAnswers == riddleCounter) {
                    incorrectAnswers = random.nextInt(riddles.size());
                }
                options.add(answers.get(incorrectAnswers));
            }
        }
        btn1.setText(options.get(0));
        btn2.setText(options.get(1));
        btn3.setText(options.get(2));
        btn4.setText(options.get(3));
    }

1 个答案:

答案 0 :(得分:0)

试试这个

      for (int i = 0; i < 4; i++) {

        if (i == locationOfCorrectAnswers && 
                    !options.contains(answers.get(riddleCounter))) {

                options.add(answers.get(riddleCounter));

        }else {

            incorrectAnswers = random.nextInt(riddles.size());

            while (incorrectAnswers == riddleCounter) {

                incorrectAnswers = random.nextInt(riddles.size());
            }

             if(!options.contains(answers.get(incorrectAnswers)))
                   options.add(answers.get(incorrectAnswers));
             else --i;
        }

    }

应该可以防止重复

编辑:添加完整代码

EDIT2:修复indexOutOfBounds异常