防止在随机播放模式下重复播放 [英] Prevent repeat in shuffle mode

查看:98
本文介绍了防止在随机播放模式下重复播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在测验中随机回答已经问过的问题(以便不再重复)?在不使应用程序崩溃的情况下实现此目标的最佳方法是什么?

How do I random out my already asked question (so that it doesn't repeat) in a quiz? What's the best possible way to achieve this without crashing the app?

问题:我只想保留一个包含所有问题的Java类,不知道在进行其他Level活动时这是否是一个好方法。因此,我一开始尝试了10个问题,但是我一直在问这些问题。然后,我将该班级分为两个(每个问题5个),分别由新级别(轻松级别1和2)定义,但问题是相同的。

Problem: I wanted to keep only one Java class with all the questions, don't know if that's a good approach when progressing towards other Level activities. So I tried it with 10 questions to start with, but I keep getting already asked questions. Then I break that class into two (5 questions each) defining each by new level (Easy Level 1 and 2) but the problem is same.

我想,这是我在逻辑部分苦苦挣扎的那一行:

I guess, this is the line I'm struggling with the logic part:

updateQuestion(rand.nextInt(mQuestionsLength)); 

这是我到目前为止尝试过的:

public class Quiz extends AppCompatActivity {
    Button answer1, answer2;
    TextView score, question, timer;
    private Question mQuestions = new Question();
    private String mExplanation;
    private String mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = 0;
    private String mQuestionsIndex;
    private int mAns = 0;
    public int mQuestionsLength = mQuestions.mQuestions.length;
    Random rand;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy_level1);

        rand = new Random();

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);

        score = (TextView) findViewById(R.id.score);
        question = (TextView) findViewById(R.id.question);
        timer = (TextView) findViewById(R.id.timer);

        score.setText("Overall Score: " + mScore);

        updateQuestion(rand.nextInt(mQuestionsLength));

        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer1.getText() == mAnswer){
                    bingoAlert();
                } else {
                    oopsAlert();
                }
            }
        });
        answer2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(answer2.getText() == mAnswer){
                    bingoAlert();
                } else {
                    oopsAlert();
                }
            }
        });
    }
    private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
        mQuestionsIndex = mQuestions.getQuestionIndex(num);
        mQuestionNumber++;
    }

    private void bingoAlert(){
        mScore+=5;
        score.setText("Score: " + mScore);
        if(mQuestionNumber == mQuestions.mQuestions.length){
            // Do something...
        }else{
            updateQuestion(rand.nextInt(mQuestionsLength));
        }
    }
    private void oopsAlert(){
        mScore-=2;
        score.setText("Score: " + mScore);
        if(mQuestionNumber == mQuestions.mQuestions.length){
            // Do something...
        }else{
            updateQuestion(rand.nextInt(mQuestionsLength));
        }
    }

问题课堂

public class Questions {

    public static String[] mQuestions = new String[]{
            "Question 1",
            "Question 2",
            "Question 3",
            "Question 4",
            "Question 5"

    };
    public static String[] mQuestionsIndex = new String[]{
            "1", "2", "3", "4", "5"
    };
    private static String mChoices[][] = {
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"},
            {"Choice 1", "Choice 2"}
    };
    private String mCorrectAnswers[] =
            {"Choice 1", "Choice 2", "Choice 1", "Choice 2", "Choice 1"};

    public String getQuestion(int a) {
        String question = mQuestions[a];
        return question;
    }

    public String getChoice1(int a) {
        String choice = mChoices[a][0];
        return choice;
    }

    public String getChoice2(int a) {
        String choice = mChoices[a][1];
        return choice;
    }

    public String getCorrectAnswer(int a) {
        String answer = mCorrectAnswers[a];
        return answer;
    }

    public String getQuestionIndex(int a) {
        String questionsIndex = mQuestionsIndex[a];
        return questionsIndex;
    }

这是我的随机课程

class RandomClass {
    private Random randNum;

    public RandomClass() {
        randNum = new Random();
    }

    public int[] generateRandomArray(int arraySize){
        int[] theArray = new int[arraySize];
        for(int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++){
            theArray[mQuestionNumber] = randNum.nextInt(6);
        }
        return theArray;
    }
}

这是我最近的尝试:

private void updateQuestion(int num){
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));

        mAnswer = mQuestions.getCorrectAnswer(num);
        mExplanation = mQuestions.getExplanation(num);
        mQuestionsIndex = mQuestions.getQuestionIndex(num);

        for (int mQuestionNumber = 0; mQuestionNumber <= 5; mQuestionNumber++) {
            int[] ints = new RandomClass().generateRandomArray(5);
        }
    }

谢谢!

推荐答案

提示:维护 ArrayList


  1. 创建一个ArrayList。

  2. 添加问题的索引。例如:列表中的1,2,3,4 ..... n。

  3. 生成List大小范围内的随机数。

  4. 显示与生成的随机数相对应的问题(最好是问题编号的索引)。

  5. 显示问题后,从列表中删除该项目。

  6. 从列表中删除项目时,列表将更新,并且删除的元素后的所有元素将上移一个位置,列表的大小将更新(减小1)。

  7. 再次在列表的大小范围内生成一个随机数,然后重复该过程。

  1. Create an ArrayList.
  2. Add the indexes of the questions. eg: 1,2,3,4.....n in list.
  3. Generate a random number within the range of size of List.
  4. Display the question corresponding to the random number generated(preferably the index of the question number).
  5. After displaying the question remove that item from the list.
  6. As you delete the item from the list, the list gets updated and all the elements after the deleted elements move up by one position and size of the list gets updated (reduced by 1).
  7. Again generate a random number within the range of size of the list and repeat the process.

list.get( random_number); 将为您提供该位置存储的数字的索引,以便您可以相应地显示位置。

list.get(random_number); will give you the index of the number stored in that position so that you can display your position accordingly.

让我知道是否有任何不清楚的地方或含糊之处。我将对其进行更新。

Let me know if any point is not clear or ambiguous. I'll update it.

这篇关于防止在随机播放模式下重复播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆