尝试使用方法创建驾驶执照考试程序

时间:2019-12-18 00:24:33

标签: java arrays

我正在尝试创建一个Java程序,该程序对驾驶执照考试的书面部分进行评分,用户需要正确回答20个问题中的15个才能通过。应该有一种方法可以允许用户输入他们的选择。包括输入验证,仅接受字母A,B,C或D。

还应该有第二种方法来评估学生的答案,显示正确答案的数量,学生是否通过了考试以及是否填充了错误答案的数量。

第三个方法将显示遗漏的问题编号。

到目前为止我所拥有的:

import java.util.Scanner;

public class DriverLicenseExam 
{
   public static void main(String[] args)
   {
   // Arrays
   String[] correctAnswers = {"B", "D", "A", "A", "C", 
                              "A", "B", "A", "C", "D", 
                              "B", "C", "D", "A", "D", 
                              "C", "C", "B", "D", "A"};
   String[] userAnswers; 
   int[] missed = new int[correctAnswers.length];
   String[] answers = new String[20]; 
   String answer; 

   // User's input
   System.out.println("Enter your answers for the exam here: "); 
   Scanner input = new Scanner(System.in);
      for (int x = 0; x < 20; x++)
      {
         do
         {
            System.out.print((x+1) + ": "); 
            answer = input.nextLine(); 
         } 
         while (!isValidAnswer(answer)); 

         answers[x] = answer; 
      }

      //Process the user's answers
          userAnswers = new String[answers.length]; 

          for (int x = 0; x < answers.length; x++)
          {
             userAnswers[x] = answers[x]; 
          }


      //Results 
      int correctCount = totalCorrect();
      int incorrectCount = totalIncorrect();
      int missedCount = questionsMissed();

      //Outputting total correct
      System.out.println("Total Correct: " + correctCount); 

      //Outputting total incorrect
      System.out.println("Total Incorrect: " + incorrectCount);

      //Outputting missed questions
      System.out.println("Total missed: " + missed);
   }

   // Determines total correct answers
      public static int totalCorrect()
   {
      int correctCount = 0; 

          for (int x = 0; x < correctAnswers.length; x++)
          {
             if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
             {
                correctCount++; 
             }
          }
          return correctCount;
   }

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

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

       // Determines total questions missed
       public int[] questionsMissed()
       {
          return missedCount; 
       }

       // Returns if user's answer is valid
       public static boolean isValidAnswer (String answer)
   {
      return "A".equalsIgnoreCase(answer) || 
         "B".equalsIgnoreCase(answer)
         || "C".equalsIgnoreCase(answer) || 
         "D".equalsIgnoreCase(answer); 
   }
}

输出:

DriverLicenseExam.java:48: error: non-static method questionsMissed() cannot be referenced from a static context
      int missedCount = questionsMissed();
                        ^
DriverLicenseExam.java:48: error: incompatible types: int[] cannot be converted to int
      int missedCount = questionsMissed();
                                       ^
DriverLicenseExam.java:65: error: cannot find symbol
          for (int x = 0; x < correctAnswers.length; x++)
                              ^
  symbol:   variable correctAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:67: error: cannot find symbol
             if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
                                                 ^
  symbol:   variable correctAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:67: error: cannot find symbol
             if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
                 ^
  symbol:   variable userAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:80: error: cannot find symbol
          for (int x = 0; x < correctAnswers.length; x++)
                              ^
  symbol:   variable correctAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:82: error: cannot find symbol
             if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
                                                  ^
  symbol:   variable correctAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:82: error: cannot find symbol
             if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
                  ^
  symbol:   variable userAnswers
  location: class DriverLicenseExam
DriverLicenseExam.java:84: error: cannot find symbol
                missed[incorrectCount] = x; 
                ^
  symbol:   variable missed
  location: class DriverLicenseExam
DriverLicenseExam.java:94: error: cannot find symbol
          return missedCount; 
                 ^
  symbol:   variable missedCount
  location: class DriverLicenseExam
10 errors

我在处理这些错误时遇到了麻烦,不知道如何解决。感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

建议很少,我想给你:  1.如评论部分中ggorlen所述,请勿尝试立即编写庞大的代码,然后对其进行测试。在编写代码时继续对其进行测试。  2.使用编辑器,有许多免费的JAVA编辑器可用来帮助您调试或识别代码中的错误。  3.请尝试阅读一些基本概念,例如作用域,静态关键字,返回类型。

您遇到的错误之一:

DriverLicenseExam.java:48: error: incompatible types: int[] cannot be converted to int
      int missedCount = questionsMissed();

因为您的方法返回一个数组,并且您将其捕获到一个int变量中。

答案 1 :(得分:0)

由于您的参数不是全局参数,因此我进行了一些修改, 您会看到:

修复您的三种方法:

    int[] missedCount = questionsMissed(missed);
    int correctCount = totalCorrect(correctAnswers,userAnswers);
    int incorrectCount = totalIncorrect(correctAnswers,userAnswers,missedCount);


import java.util.Scanner;
public class DriverLicenseExam
{

public static void main(String[] args) { // Arrays String[] correctAnswers = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}; String[] userAnswers; int[] missed = new int[correctAnswers.length]; String[] answers = new String[20]; String answer; // User's input System.out.println("Enter your answers for the exam here: "); Scanner input = new Scanner(System.in); for (int x = 0; x < 20; x++) { do { System.out.print((x+1) + ": "); answer = input.nextLine(); } while (!isValidAnswer(answer)); answers[x] = answer; } //Process the user's answers userAnswers = new String[answers.length]; for (int x = 0; x < answers.length; x++) { userAnswers[x] = answers[x]; } int[] missedCount = questionsMissed(missed); //Results int correctCount = totalCorrect(correctAnswers,userAnswers); int incorrectCount = totalIncorrect(correctAnswers,userAnswers,missedCount); //Outputting total correct System.out.println("Total Correct: " + correctCount); //Outputting total incorrect System.out.println("Total Incorrect: " + incorrectCount); //Outputting missed questions System.out.println("Total missed: " + missed); } // Determines total correct answers public static int totalCorrect(String[] correctAnswers,String[] userAnswers) { int correctCount = 0; for (int x = 0; x < correctAnswers.length; x++) { if (userAnswers[x].equalsIgnoreCase(correctAnswers[x])) { correctCount++; } } return correctCount; } // Determines total incorrect answers public static int totalIncorrect(String[] correctAnswers,String[] userAnswers,int[] missedCount) { int incorrectCount = 0; for (int x = 0; x < correctAnswers.length; x++) { if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x])) { missedCount[incorrectCount] = x; incorrectCount++; } } return incorrectCount; } // Determines total questions missed public static int[] questionsMissed(int[] missedCount) { return missedCount; } // Returns if user's answer is valid public static boolean isValidAnswer (String answer) { return "A".equalsIgnoreCase(answer) || "B".equalsIgnoreCase(answer) || "C".equalsIgnoreCase(answer) || "D".equalsIgnoreCase(answer); } }