Java-在测验应用程序中实现3级难度
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-在测验应用程序中实现3级难度,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4376字,纯文字阅读大概需要7分钟。
内容图文
![Java-在测验应用程序中实现3级难度](/upload/InfoBanner/zyjiaocheng/675/735d6b232d7d4de7a59f394a85ffe782.jpg)
我正在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级难度所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。