我正在Android上开发一个简单的测验应用程序.随着测验的进行,测验中的问题应变得更加困难.现在,我正在尝试实现一个静态方法,该方法采用一个Question对象列表,并根据难度生成(选择)相应问题的子列表,并且应该对其进行排序(列表中的第一个问题是最简单的) .该应用程序具有3个难度级别(模式).
这是方法片段:
public static List<Question> getQuestions(List<Question> availableQuestions, int quizDifficulty, int numberOfQuestion){ if(availableQuestions.size() < numberOfQuestion) throw NotEnoughQuestionsException(); List<Question> questions = new ArrayList<Question>(numberOfQuestion); if(quizDifficulty == 0) // Easy { // ... return questions; } else if(quizDifficulty == 2) // Hard { // ... return questions; } else /*if(quizDifficulty == 1)*/ // normal { // ... return questions; }}
每个Question对象的字段难度都在1(最简单)到10(最困难)的范围内,并且可以使用getDifficulty()方法访问此字段.
由于我是实现该方法的一种方法,因此我决定使问题的难度不超过Easy模式的8级,并且在Hard模式下应该大于3级,并且在9级和2级之间普通模式.
问题在于所提供的可用问题列表不能保证包含所有要求的难度级别,例如,所有问题都在级别1中.
因此,我的问题是,实现此方法的最佳想法是什么?
编辑:
到目前为止,这是我的进度:
public static List<Question> getQuestions(List<Question> availableQuestions, int quizDifficulty, int numberOfQuestion){ if(availableQuestions.size() < numberOfQuestion) throw NotEnoughQuestionsException(); List<Question> questions = new ArrayList<Question>(numberOfQuestion); Map<Integer, List<Question>> map = new HashMap<Integer, List<Question>>(); for(int i = 1; i <= 10; i++) map.put(i, new ArrayList<Question>()); for(Question question : availableQuestions) map.get(question.getDifficulty()).add(question); int L1 = map.get(1).size(); // number of questions with level 1 int L2 = map.get(2).size(); int L3 = map.get(3).size(); int L4 = map.get(4).size(); int L5 = map.get(5).size(); int L6 = map.get(6).size(); int L7 = map.get(7).size(); int L8 = map.get(8).size(); int L9 = map.get(9).size(); int L10 = map.get(10).size(); final int L1_TO_L8 = 0; final int L1_TO_L9 = 1; final int L1_TO_L10 = 2; final int L2_TO_L9 = 3; final int L2_TO_L10 = 4; final int L3_TO_L10 = 5; int status; if(difficulty == 0) // Easy (level 1 to level 8) { int missing = questionsCount - (L1+L2+L3+L4+L5+L6+L7+L8); if(missing > 0) // not enough questions in L1 through L8 { if(missing - L9 > 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L1 through L9 { status = L1_TO_L9; } } else // enough questions in L1 through L8 { status = L1_TO_L8; } } else if(difficulty == 2) // Hard (level 3 to level 10) { int missing = questionsCount - (L3+L4+L5+L6+L7+L8+L9+L10); if(missing > 0) // not enough questions in L3 through L10 { if(missing - L2 > 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L2 through L10 { status = L2_TO_L10; } } else // enough questions in L3 through L10 { status = L3_TO_L10; } } else /*if(difficulty == 1)*/ // normal (level 2 to level 9) { int missing = questionsCount - (L2+L3+L4+L5+L6+L7+L8+L9); if(missing > 0) // not enough questions in L2 through L9 { if(missing - L1 > 0) // we must include all the level { status = L1_TO_L10; } else // enough questions in L1 through L9 { status = L1_TO_L9; } } else // enough questions in L2 through L9 { status = L2_TO_L9; } } // ...}
解决方法:
最简单的解决方案是根据您的模式获取所有具有一定级别的问题,然后可以对该列表进行排序,例如:
public static List<Question> getQuestions(List<Question> availableQuestions, int quizDifficulty, int numberOfQuestion) { if(availableQuestions.size() < numberOfQuestion) throw NotEnoughQuestionsException(); List<Question> questionsForusermode = getQuestionsFromMode( availableQuestions, quizDifficulty); // sort this questionsForusermode by Difficulty using comprator } // please put these magic numbers in constant fIElds or enum :) public static List<Question> getQuestionsFromMode (List<Question> questions, int mode) { if ( mode == 1 ) { return getQuestionsWithCertainLevel(questions, 1, 8); } else if ( moode == 2 ) { return getQuestionsWithCertainLevel(questions, 2, 9); } else return getQuestionsWithCertainLevel(questions, 3, 10); } private static List<Question> getQuestionsWithCertainLevel( List<Question> questions, int fromLeve, int tolevel) { List<Question> subQuestions = new ArrayList<Question>(); for(Question question: questions) { if ( question.getDifficulty() >= fromLevel && question.getDifficulty() <= tolevel ) { subQuestions.add(question); } } return subQuestions; }
总结 以上是内存溢出为你收集整理的Java-在测验应用程序中实现3级难度全部内容,希望文章能够帮你解决Java-在测验应用程序中实现3级难度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)