我正在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对象都有一个difficulty字段,该字段在1 (most simple)10 (most difficult)的范围内,并且可以通过getDifficulty()方法访问此字段。

尽管我是实现该方法的一种方法,但我决定使问题的难度不应该超过8模式下的Easy级别,并且应该超过3模式下的Hard级别,以及在9级别和2级别之间的难度。 Normal模式。

问题是,不能保证所提供的问题availableQuestions列表包含所有必需的难度级别,例如,所有问题都在级别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;
  }

08-08 06:29