0

我正在编写一个程序,要求用户进行测验。我必须阅读包含我需要的信息的文本文件。但是,我遇到了如何做到这一点的麻烦。我已经将尽可能多的问题分类到一个列表数组中,但其余的问题给我带来了麻烦。这是我必须使用的文本文件的格式:

(问题)(选项中的答案数量)(x 选项)(尝试次数)(正确次数)(错误次数)

我将如何使用我必须区分答案整数和统计整数的代码?

    Scanner fScan = new Scanner(new File(file_name));


    List<String> questions = new ArrayList<String>();
    List<String> other = new ArrayList<String>();
    int[] answers = new int[questions.size()];



    while (fScan.hasNextLine()) 
    {

        String line = fScan.nextLine();

    if (line.contains("?")) 
    {
        questions.add(line);

    } 
    else 
    {
        other.add(line);
    }
}
4

1 回答 1

0

最好创建一个Map以问题为键和答案列表为值的方法,例如:

Map<String, List<String>> questionAnswersMap = new HashMap<String, List<String>>();
List<String> answers = null;
while (fScan.hasNextLine()) {
   String line = fScan.nextLine();
   if (line.contains("?")){
      //new Question
      answers = new ArrayList<String>()
      questionAnswersMap.put(line, answers );
   }else{
      //answer of the previous question
      answers.add(line);
   } 
}

在这里,我假设问题和答案是按顺序编写的。

于 2012-11-02T04:20:00.737 回答