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

时间:2018-03-04 05:14:52

标签: android arrays random

如何在测验中随机输出我已经提出的问题(以便它不会重复)?在不崩溃应用程序的情况下实现此目标的最佳方法是什么?

问题:我想只保留一个带有所有问题的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);
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

提示:维护ArrayList

  1. 创建一个ArrayList。
  2. 添加问题的索引。例如:1,2,3,4 ..... n在列表中。
  3. 生成List大小范围内的随机数。
  4. 显示与生成的随机数对应的问题(最好是问题编号的索引)。
  5. 显示问题后,从列表中删除该项目。
  6. 当您从列表中删除项目时,列表会更新,并且已删除元素之后的所有元素都会向上移动一个位置,并且列表的大小会更新(减少1)。
  7. 再次生成列表大小范围内的随机数并重复该过程。

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

  8. 如果有任何不清楚或含糊不清的地方,请告诉我。我会更新它。