如何根据您所在的级别从列表中选择几乎随机的问题?

我正在做一个项目,我想根据你所处的级别来回答不同的问题。

例如,前 25 个级别我希望我的系统选择简单的问题,例如 80% 的机会和 20% 的中等问题。在 80 级结束时,我希望难度慢慢增加,系统将 100% 只选择难题。我怎样才能编写一个工作缓慢增加的图表来实现我的arraypicker?

我试图用以下方法洗牌ArrayList对象:

Collections.shuffle(random);

例如为了获得百分比,但我应该有一个更简单的解决方法。

很难解释,但我希望你能理解,请问你是否需要更多信息。我正在 Android Studio 上开发一个 libgdx 项目


交互式爱情
浏览 151回答 1
1回答

慕婉清6462132

您有几个问题包含在一个问题中,使您的问题对于本网站来说有点宽泛,但让我们分解您的问题:创建一个具有不同难度级别的问题类。让我们称这个类Question和难度级别,Difficulty创建一个类,将这些问题保存在一个或多个集合中,并允许用户从这个类中请求一个随机问题。让我们调用这个类QuestionCollection和用于请求随机问题的方法public Question getRandomQuestion(...)。让用户有自己的进步水平。这可以是User类可能从请求中收到的问题的分布getRandomQuestion(...)将取决于用户的进步水平所以首先要封装难度级别,让我们创建一个枚举,它有 3 个(或更多)级别:public enum Difficulty {&nbsp; &nbsp; EASY, MEDIUM, HARD}然后 Question 类可以有一个private Difficulty difficulty;字段,在其构造函数中设置一个字段,并使用公共 getter 方法public Difficulty getDifficulty()。简化的 Question 类可能如下所示:public class Question {&nbsp; &nbsp; private String question;&nbsp; &nbsp; private String answer;&nbsp; &nbsp; private Difficulty difficulty;&nbsp; &nbsp; public Question(String question, String answer, Difficulty difficulty) {&nbsp; &nbsp; &nbsp; &nbsp; this.question = question;&nbsp; &nbsp; &nbsp; &nbsp; this.answer = answer;&nbsp; &nbsp; &nbsp; &nbsp; this.difficulty = difficulty;&nbsp; &nbsp; }&nbsp; &nbsp; public String getQuestion() {&nbsp; &nbsp; &nbsp; &nbsp; return question;&nbsp; &nbsp; }&nbsp; &nbsp; public String getAnswer() {&nbsp; &nbsp; &nbsp; &nbsp; return answer;&nbsp; &nbsp; }&nbsp; &nbsp; public Difficulty getDifficulty() {&nbsp; &nbsp; &nbsp; &nbsp; return difficulty;&nbsp; &nbsp; }}同样,这一切都过于简单化了,但它可以用来帮助说明问题和可能的解决方案。如果需要,您可以让此类实现Comparable<Question>,并使用难度来帮助进行比较,并允许您List<Question>按难度对 a 进行排序。那么所有这一切的关键将是QuestionCollection类,它拥有问题的集合并拥有getRandomQuestion(...)方法——如何实现这一点。一种方法是,在这一点上,与其担心用户的进步水平,不如给getRandomQuestion(...)方法一些参数,让QuestionCollection他们知道要使用什么分布。在我看来,最简单的方法是给它一个相对的难度频率,一个, 和的百分比Difficulty.EASY,例如:Difficulty.MEDIUMDifficulty.HARDpublic Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard) {&nbsp; &nbsp; // ... code here to get the random question}好的,现在我们正在讨论如何构建类的内部工作QuestionCollection,然后使用它来根据参数百分比获得随机问题的正确分布。可能最简单的方法是将一个问题放入它自己的列表中,例如基于其难度级别的 ArrayList - 所以这里有 3 个列表,一个用于 EASY,一个用于 MEDIUM,一个用于 HARD 问题。所以:private List<Question> easyQuestions = new ArrayList<>();private List<Question> mediumQuestions = new ArrayList<>();private List<Question> hardQuestions = new ArrayList<>();或者另一个可能更清洁的解决方案是使用一个Map<Difficulty, List<Question>>而不是单独的列表,但我现在将保持简单并将其保留为 3 个列表。然后该类将有一个public void addQuestion(Question q)方法可以根据难度级别将问题添加到正确的列表中:public void addQuestion(Question q) {&nbsp; &nbsp; switch (q.getDifficulty()) {&nbsp; &nbsp; case EASY:&nbsp; &nbsp; &nbsp; &nbsp; easyQuestions.add(q);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case MEDIUM:&nbsp; &nbsp; &nbsp; &nbsp; mediumQuestions.add(q);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case HARD:&nbsp; &nbsp; &nbsp; &nbsp; hardQuestions.add(q);&nbsp; &nbsp; }}好的,所以我们的列表充满了问题,我们现在的核心问题是如何获得正确的随机分布?我会推荐一个两步过程——首先使用Math.random()Random 类的一个实例来选择从哪个列表中获取问题,然后使用随机化从所选列表中选择一个随机问题。所以第一步,获取随机列表可能如下所示:// declare variable before the if blocksList<Question> randomList = null;// get a random int from 0 to 99int rand = (int) (100 * Math.random());// get the random list using basic math and if blocksif (rand < percentEasy) {&nbsp; &nbsp; randomList = easyQuestions;} else if (rand < percentEasy + percentMedium) {&nbsp; &nbsp; randomList = mediumQuestions;} else {&nbsp; &nbsp; randomList = hardQuestions;}OK,一旦获得了 randomList,然后从中获取一个随机问题:// first get a random index to the list from 0 to < sizeint size = randomList.size();int listIndex = (int)(size * Math.random());Question randomQuestion = randomList.get(listIndex);return randomQuestion;整个QuestionCollection类(简化版)可能如下所示:// imports herepublic class QuestionCollection {&nbsp; &nbsp; private List<Question> easyQuestions = new ArrayList<>();&nbsp; &nbsp; private List<Question> mediumQuestions = new ArrayList<>();&nbsp; &nbsp; private List<Question> hardQuestions = new ArrayList<>();&nbsp; &nbsp; public void addQuestion(Question q) {&nbsp; &nbsp; &nbsp; &nbsp; switch (q.getDifficulty()) {&nbsp; &nbsp; &nbsp; &nbsp; case EASY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easyQuestions.add(q);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MEDIUM:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mediumQuestions.add(q);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case HARD:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hardQuestions.add(q);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard) {&nbsp; &nbsp; &nbsp; &nbsp; // if the numbers don't add up to 100, the distribution is broken -- throw an exception&nbsp; &nbsp; &nbsp; &nbsp; if (percentEasy + percentMedium + percentHard != 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = String.format(format, percentEasy, percentMedium, percentHard);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(text);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; List<Question> randomList = null;&nbsp; &nbsp; &nbsp; &nbsp; int rand = (int) (100 * Math.random());&nbsp; &nbsp; &nbsp; &nbsp; if (rand < percentEasy) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; randomList = easyQuestions;&nbsp; &nbsp; &nbsp; &nbsp; } else if (rand < percentEasy + percentMedium) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; randomList = mediumQuestions;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; randomList = hardQuestions;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // we've now selected the correct List&nbsp; &nbsp; &nbsp; &nbsp; // now get a random question from the list:&nbsp; &nbsp; &nbsp; &nbsp; // first get a random index to the list from 0 to < size&nbsp; &nbsp; &nbsp; &nbsp; int size = randomList.size();&nbsp; &nbsp; &nbsp; &nbsp; int listIndex = (int)(size * Math.random());&nbsp; &nbsp; &nbsp; &nbsp; Question randomQuestion = randomList.get(listIndex);&nbsp; &nbsp; &nbsp; &nbsp; return randomQuestion;&nbsp; &nbsp; }}因此,为了测试概念证明,一个测试程序显示该分布有效:// importspublic class QuestionFun {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // create QuestionCollection object&nbsp; &nbsp; &nbsp; &nbsp; QuestionCollection questionCollection = new QuestionCollection();&nbsp; &nbsp; &nbsp; &nbsp; // fill it with questions with random difficulty&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 1000; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String question = "Question #" + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String answer = "Answer #" + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int randomIndex = (int) (Difficulty.values().length * Math.random());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Difficulty difficulty = Difficulty.values()[randomIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question q = new Question(question, answer, difficulty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questionCollection.addQuestion(q);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);&nbsp; &nbsp; &nbsp; &nbsp; for (Difficulty diff : Difficulty.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frequencyDistMap.put(diff, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int easyPercent = 20;&nbsp; &nbsp; &nbsp; &nbsp; int mediumPercent = 70;&nbsp; &nbsp; &nbsp; &nbsp; int hardPercent = 10;&nbsp; &nbsp; &nbsp; &nbsp; int questionCount = 10000;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < questionCount; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Difficulty difficulty = q.getDifficulty();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int currentCount = frequencyDistMap.get(difficulty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frequencyDistMap.put(difficulty, currentCount);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Difficulty: Count (Percent)");&nbsp; &nbsp; &nbsp; &nbsp; String format = "%-12s %4d&nbsp; &nbsp;(%02d)%n";&nbsp; &nbsp; &nbsp; &nbsp; for (Difficulty difficulty : Difficulty.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = frequencyDistMap.get(difficulty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int percent = (int) Math.round((100.0 * number) / questionCount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(format, difficulty + ":", number, percent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}返回原始分布百分比:Difficulty: Count (Percent)EASY:&nbsp; &nbsp; &nbsp; 200325&nbsp; &nbsp;(20)MEDIUM:&nbsp; &nbsp; 699341&nbsp; &nbsp;(70)HARD:&nbsp; &nbsp; &nbsp; 100334&nbsp; &nbsp;(10)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java