防止在随机播放模式下重复播放

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了防止在随机播放模式下重复播放相关的知识,希望对你有一定的参考价值。

如何在测验中随机抽出我已经问过的问题(这样就不重复了)?在不破坏应用程序的情况下实现此目标的最佳方法是什么?

问题:我想只保留一个带有所有问题的Java类,不知道在进行其他Level活动时这是否是一个好方法。所以我开始尝试10个问题,但我一直在问问题。然后我把这个类分成两个(每个5个问题),用新级别定义每个级别(Easy Level 1和2),但问题是相同的。

我想,这就是我在逻辑部分挣扎的路线:

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. 再次生成列表大小范围内的随机数并重复该过程。 list.get(random_number);将为您提供存储在该位置的数字的索引,以便您可以相应地显示您的位置。

如果有任何意见不明确或含糊不清,请告诉我。我会更新它。

以上是关于防止在随机播放模式下重复播放的主要内容,如果未能解决你的问题,请参考以下文章

按下按钮时播放随机声音[重复]

在片段中单击按钮播放声音(Kotlin)[重复]

如何播放下一首歌曲的随机歌曲[重复]

HTML 音频 - 通过单击 [重复] 随机播放

如何在不重复单词并遍历整个数组的情况下从数组中随机播放文本? (迅速)

即使在静音模式下也能在 iPhone 上播放声音 [重复]