0

我需要帮助找出我的代码出了什么问题。我试图使用 driver1.DriverExam(answers) 来调用 object.method(array) 希望它能够正确地对用户的输入进行评分,但这会产生错误。如何在第二个文件中正确调用 driver1.DriverExam(answers) 以从第一个文件执行 DriverExam 构造函数?我敢肯定这是一些简单的修复,你们都会在看到它的一分钟内注意到,但我似乎无法弄清楚。先感谢您!

到目前为止我有这个(一个文件要处理,另一个作为用户输入):

/**
   DriverExam class
   Chapter 8, Programming Challenge 5
*/

public class DriverExam
{
   final int PASSING = 15;       // Minimum # of correct answers to pass
   // Array of correct answers
   private char[] correct = { 'B', 'D', 'A', 'A',
                              'C', 'A', 'B', 'A',
                              'C', 'D', 'B', 'C',
                              'D', 'A', 'D', 'C',
                              'C', 'B', 'D', 'A' };

    //step 1: declare an array to hold the student's answers
    private char[] studentAnswers = new char[20];           

    //step 2: declare two int variables, one is to hold the number of correct answers.
   // The other is to hold the number of incorrect answers. 
    private int correctAnswers = 0;
    private int incorrectAnswers = 0;

    /**
   step 3: The constructor copies an array of answers
      to the student field.
      @param s The array of answers to copy.
   */
   public DriverExam(char[] answers)
    {
        studentAnswers = answers;
    }

   /**
    step 4: The gradeExam method determines the number of
      correct and incorrect answers.   */
   public void gradeExam()
    {
        for (int k = 0; k < correct.length; k++)
        {
            if (studentAnswers[k] == correct[k])
            {
                correctAnswers += 1;
            }
            else
            {
                incorrectAnswers += 1;
            }
        }
    }

    /**
     step 5: The passed method determines whether the student
      passed or failed the exam.
      @return true if the student passed, false otherwise.
   */
    public boolean passed()
    {
        if (correctAnswers >= 15)
        {
            return true;
        }
        return false;
    }

   /**
    step 6: The totalCorrect method returns the number of
      questions correctly answered.
      @return The number of questions correctly answered.
   */
    public char[] totalCorrect()
    {
        return correctAnswers;
    }

}






/**
   DriverTest program
   Chapter 8, Programming Challenge 5
*/

import java.util.Scanner;
public class DriverTest
{
   public static void main(String[] args) 
   {
      String input;                             // To hold keyboard input
      final int NUM_ANSWERS = 20;               // Number of answers
      char[] answers = new char[NUM_ANSWERS];   // Array to hold answers


      // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Get the user's answers to the questions.
      System.out.println("Enter your answers to the " +
                         "exam questions. (Make sure " +
                         "Caps Lock is ON!)");
        //step 7: Get the student's answers to the questions
        for (int x = 0; x < NUM_ANSWERS; x++)
        {
            System.out.print("Enter answer to question " + (x + 1) + ": ");
            answers[x] = keyboard.next().charAt(0);
        }

      //step 8: Create a DriverExam object.
        DriverExam driver1 = new DriverExam(answers);

        //step 9: Display a report to print the number of correct answers,  
        //the number of incorrect answers, and whether the student passes the exam or not by calling
        //gradeExam, totalCorrect, and passed methods.
        driver1.gradeExam();    
        System.out.println("The number of correct answers is: " + driver1.totalCorrect());
        System.out.println("The number of incorrect answers is: " + (NUM_ANSWERS - driver1.totalCorrect()));
        if (driver1.passed() == true)
        {
            System.out.println("Congratulations! You have passed!");
        }
        else 
        {
            System.out.println("You have failed.");
        }

    }
  }
4

2 回答 2

1

我看到两个问题,首先..

public char[] totalCorrect() {
    return correct;
}

我认为它应该correctAnswers根据您对该功能的使用情况返回并对其进行评论。请记住,您还需要更改返回类型。第二,创建DriverExam对象的方式。它应该是

DriverExam driver1 = new DriverExam(answers);
于 2013-04-21T03:23:41.113 回答
1

您应该使用允许您传递答案的 DriverExam 构造函数,而不是您正在执行的默认构造函数。

即,不是这个:

    DriverExam driver1 = new DriverExam();

    driver1.DriverExam(answers); // this makes no sense and is not legal Java

但是这个:

    DriverExam driver1 = new DriverExam(answers);
于 2013-04-21T03:18:38.120 回答