【问题标题】:Trying to create a drivers license exam program using methods尝试使用方法创建驾驶执照考试程序
【发布时间】:2019-12-18 00:24:33
【问题描述】:

我正在尝试创建一个 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

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

【问题讨论】:

  • 目前correctAnswers 的范围仅限于main 方法(因为那是您声明它的地方)。如果您想在其他方法中使用它,您需要将其声明为字段将其作为参数传递给其他方法。
  • userAnswers 也有同样的问题。
  • 除了上面所说的之外,这里有一个通用的提示:在编码时,不要只是在运行代码之前先写3-4个函数。到那时,你已经积累了几十个错误,解开所有问题变得令人沮丧。如果你每改几行就编译运行代码,我想你会发现当你立即发现问题时,问题变得更加明显和可解决。

标签: java arrays


【解决方案1】:

有几个建议,我想给你: 1.正如ggorlen在评论部分提到的,不要试图一次写大代码然后测试它。在编写代码时继续测试代码。 2. 使用编辑器,有很多免费的 JAVA 编辑器可以帮助您调试或识别代码中的错误。 3. 请尝试阅读一些基本概念,如作用域、静态关键字、返回类型。

您遇到的错误之一:

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

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

【讨论】:

    【解决方案2】:

    因为你的参数不是全局的,所以我做了一些修改, 你可以看到:

    修复你的三个方法:

        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);
    }
    }
    

    【讨论】:

    • 一个好的答案不仅仅是代码,还包括一些关于你做了什么以及为什么做的解释。这也值得一读:How do I write a good answer?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    相关资源
    最近更新 更多