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