1. ホーム
  2. java

[解決済み] Javaドライバーライセンスプログラム - どの問題を見逃したか表示されない問題がある。

2022-02-27 20:11:29

質問

以下の手順でプログラムを作成しました。プログラムを実行すると、私が見落とした不正解の出力が不正確です。例えば、19番以外の正解を入力した場合、出力データの不正解の部分に1または2と表示されます。何がいけなかったのでしょうか?


地元の運転免許所から、免許試験の筆記部分を採点するプログラムを作成するよう依頼されました。試験には20問の多肢選択問題があります。受験者は、20問中15問正解すれば合格です。以下はその正解です。

1. B    2. D    3. A    4. A
5. C    6. A    7. B    8. A
9. C    10. D   11.B    12. C
13. D   14. A   15. D   16. C
17. C   18. B   19. D   20. A

Input validation: only accept the letters A, B, C, or D as answers

以下は、私の出力データです。

    Driver's License Exam 
 20 Multiple-Choice Questions 
       Mark A, B, C, D   
1: B
2: D
3: A
4: A
5: C
6: A
7: B
8: A
9: C
10: D
11: B
12: C
13: D
14: A
15: D
16: C
17: C
18: B
19: D
20: C
  RESULTS  
Total Correct: 19
Total Incorrect: 1
Passed: YES
The incorrect answers are: 
 2


public class DriverExam
{
   //An array containing a student's answers
   private String[] correctAnswers = 
                                 {"B", "D", "A", "A", "C", "A", 
                                  "B", "A", "C", "D", 
                                  "B", "C", "D", "A", 
                                  "D", "C", "C", "B", "D", "A"}; 
                                  
   //Store the user's answers
   private String[] userAnswers; 
   int[] missed = new int[correctAnswers.length]; 
   
   //Process the user's answers
   public DriverExam (String[] Answers)
   {
      userAnswers = new String[Answers.length]; 
      
      for (int i = 0; i < Answers.length; i++)
      {
         userAnswers[i] = Answers[i]; 
      }
   }
   
   //Returns a boolean value if correct answers > 15 
   public boolean passed()
   {
      if (totalCorrect() >= 15)
         return true; 
      else
         return false; 
   }
   
   //Determines the total correct answers
   public int totalCorrect()
   {
      int correctCount = 0; 
      
      for (int i = 0; i < correctAnswers.length; i++)
      {
         if (userAnswers[i].equalsIgnoreCase(correctAnswers[i]))
         {
            correctCount++; 


            }
          }
          return correctCount; 
       }
       
       //Determines the total incorrect answers
       public int totalIncorrect()
       {
          int incorrectCount = 0; 
          
          for (int i = 0; i < correctAnswers.length; i++)
          {
             if (!userAnswers[i].equalsIgnoreCase(correctAnswers[i]))
             {
                missed[incorrectCount] = 1; 
                incorrectCount++; 
             }
          }
          return incorrectCount; 
       }
    
       //Missed questions
       public int[] questionsMissed()
       {
          return missed; 
       }
       
    }
    //end of DriverExam class

----------------------------------------------------------------------------


import java.util.Scanner; 

public class DriverExamApplication
{
   public static void main(String[] args)
   {
      System.out.println("    Driver's License Exam "); 
      Scanner input = new Scanner(System.in); 
      
      System.out.println(" 20 Multiple-Choice Questions "); 
      System.out.println("       Mark A, B, C, D   "); 
                                       
      //Inputting string
      String[] answers = new String[20]; 
      String answer; 
      
      for (int i = 0; i < 20; i++)
      {
         do
         {
            System.out.print((i+1) + ": "); 
            answer = input.nextLine(); 
         } while (!isValidAnswer(answer)); 
         
         answers[i] = answer; 
      }
      
      //Process
      DriverExam exam = new DriverExam(answers); 
      
      //Results
      System.out.println("  RESULTS  "); 
      
      //Outputting total correct
      System.out.println("Total Correct: " + exam.totalCorrect()); 
      
      //Outputting total incorrect
      System.out.println("Total Incorrect: " + exam.totalIncorrect()); 
      
      String passed = exam.passed() ? "YES" : "NO"; 
      
      //Result pass or fail
      System.out.println("Passed: " + passed); 
      
      if (exam.totalIncorrect() > 0)
      {
          System.out.println("The incorrect answers are: "); 
          
          int missedIndex; 
          
          for (int i = 0; i < exam.totalIncorrect(); i++)
          {
            missedIndex = exam.questionsMissed()[i]+1; 
            System.out.print(" " + missedIndex); 
          }
      }
   } //end of main function
   
   //Returns true when answer is valid
   public static boolean isValidAnswer (String answer)
   {
      return "A".equalsIgnoreCase(answer) || 
         "B".equalsIgnoreCase(answer)
         || "C".equalsIgnoreCase(answer) || 
         "D".equalsIgnoreCase(answer); 
   }
} //end of Test class

解決方法は?

問題は関数 totalIncorrect を常に代入しています。 1 の代わりに i . これでうまくいくはずです。

   //Determines the total incorrect answers
   public int totalIncorrect()
   {
      int incorrectCount = 0; 

      for (int i = 0; i < correctAnswers.length; i++)
      {
         if (!userAnswers[i].equalsIgnoreCase(correctAnswers[i]))
         {
            missed[incorrectCount] = i; 
            incorrectCount++; 
         }
      }
      return incorrectCount; 
   }