Android随机活动,无需重复 [英] Android random activites without repetition

查看:60
本文介绍了Android随机活动,无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个问答游戏,在该游戏中,我必须随机回答有关问题,以避免按相同顺序回答问题.我已经通过使用switch

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

但是,问题是我可能会回到已经回答的问题,所以现在我必须编写一些代码,以避免玩家通过随机数生成器来处理先前的问题.

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);           

这会带来随机的单击按钮活动,但是我想禁用以前访问的问题,这意味着,如果一个人回答了Question002类的问题,那么他一定不能(永远也永远不会)再次遇到这个问题,因为这将使他从先前的问题中获得加分.如何仅一次单击按钮就可以随机获得问题,使问题不再出现?我希望我的问题能被理解.

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. How do I randomly get questions on button click only ONCE, so they won't appear again? I hope my question is understood.

每个问题都存储在其自己的类中(Question001,Question002 .... Question009)

Every question is stored in its own class (Question001, Question002.... Question009)

推荐答案

请按照以下步骤操作:

  1. 创建一个ArrayList(或简单地为List),然后将所有问题添加到其中.请记住,这仅包含改组的问题.请勿将其用于其他任何目的.
  2. 然后使用Collections.shuffle(question)进行随机播放.
  3. 在顶部提问(question.get(0));
  4. 回答后,将其从列表中删除.这样可以确保它不再显示.
  5. 重复步骤2-5,直到您的列表大小大于零.

让我知道是否不清楚.

这篇关于Android随机活动,无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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