我需要帮助找出我的代码有什么问题。我试图使用driver1.DriverExam(答案)调用object.method(数组),希望它能够正确评级用户的输入,但这会产生错误。如何在第二个文件中正确调用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.");
}
}
}
答案 0 :(得分:1)
你应该使用DriverExam构造函数,它允许你传递答案,而不是你正在做的默认构造函数。
即,不是这样:
DriverExam driver1 = new DriverExam();
driver1.DriverExam(answers); // this makes no sense and is not legal Java
但是这个:
DriverExam driver1 = new DriverExam(answers);
答案 1 :(得分:1)
我看到两个问题,首先......
public char[] totalCorrect() {
return correct;
}
我认为应根据您对该功能的使用情况返回correctAnswers
并对其进行评论。请记住,您还需要更改返回类型。其次,您创建DriverExam
对象的方式。它应该是
DriverExam driver1 = new DriverExam(answers);