gpt4 book ai didi

java - 使用对象调用方法

转载 作者:行者123 更新时间:2023-12-02 07:05:51 25 4
gpt4 key购买 nike

我需要帮助找出我的代码出了什么问题。我试图使用 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.");
}

}
}

最佳答案

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

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

我认为它应该根据您对该函数的使用情况返回正确答案并对其进行评论。请记住,您还需要更改返回类型。其次,创建 DriverExam 对象的方式。应该是

DriverExam driver1 = new DriverExam(answers);

关于java - 使用对象调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com