简体   繁体   English

Android随机活动,无需重复

[英]Android random activites without repetition

I am developing a quiz game, where I have to get random activities on answering the questions as to avoid the questions in same order. 我正在开发一个问答游戏,在该游戏中,我必须随机回答问题,以避免按相同顺序回答问题。 I have fixed this by using switch 我已经通过使用switch解决了这个问题

However, the problem is that I may return to questions which I've already answered, so I now have to code something that avoids the player to go to previous questions by the random generator. 但是,问题在于我可能会回到已经回答的问题,因此现在我必须编写一些代码,以免玩家使用随机数生成器来处理先前的问题。

I've done this so far; 到目前为止,我已经做到了;

  Random rand = new Random();
  int number = rand.nextInt(10);
  Intent intent = null;

       switch(number){
       case 0: intent = new Intent(MainActivity.this, Question001.class);
       break;
       case 1: intent = new Intent(MainActivity.this, Question002.class);
       break;

       //etc....
       }
       startActivity(intent);           

This brings randomly activities on button click, however I want to disable previously visited questions meaning, if a person has answered the question from class Question002, he must not be able to (never and never) get this question once more, as this will result in his gaining of extra points from earlier questions. 这会带来随机的单击按钮活动,但是我想禁用以前访问的问题,这意味着,如果一个人回答了Question002类的问题,那么他一定不能(永远也永远不会)再次遇到这个问题,因为这将导致他从先前的问题中获得了加分。 How do I randomly get questions on button click only ONCE, so they won't appear again? 如何仅一次单击按钮就可以随机获得问题,使问题不再出现? I hope my question is understood. 希望我的问题能被理解。

Every question is stored in its own class (Question001, Question002.... Question009) 每个问题都存储在其自己的类中(Question001,Question002 .... Question009)

Follow these steps: 跟着这些步骤:

  1. Create an ArrayList (or a List simply) and add all your Questions to it. 创建一个ArrayList(或简单地为List),然后将所有问题添加到其中。 Remember this only holds the questions for shuffling. 请记住,这仅包含改组的问题。 Do not use it for any other purpose. 请勿将其用于任何其他目的。
  2. Then do a shuffle using Collections.shuffle(question). 然后使用Collections.shuffle(question)进行随机播放。
  3. Ask the question on top (question.get(0)); 首先提出问题(question.get(0));
  4. After this has been answered remove it from the List. 回答之后,将其从列表中删除。 This will ensure it is never shown again. 这将确保不再显示它。
  5. Repeat steps 2 - 5 till your List size is greater than zero. 重复步骤2-5,直到您的列表大小大于零。

Let me know if it is unclear. 让我知道是否不清楚。

use 采用

ArrayList<Integer> number = new ArrayList<Integer>();
for (int i = 1; i <= 10; ++i) number.add(i);
Collections.shuffle(number);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM