gpt4 book ai didi

用于测试类的 Java Bean

转载 作者:行者123 更新时间:2023-12-01 12:23:15 29 4
gpt4 key购买 nike

我得到了一个 DrivingTestM.java 类来测试我编写的 2 个类。这是我写的类(class)。当我运行 DrivingTestM.java 时,它给我一个错误:

System.out.println( question.getDescription() );

我不确定可能是什么错误。任何人都可以尝试阐明这个错误吗?谢谢!

问题.java:

public class Question {
String description;
String answerA;
String answerB;
String answerC;
int correctAnswer;
int answer;
Boolean answerCorrect;

public Question(){

}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer, int answer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
this.answer = answer;

}
public Question(String description, String answerA, String answerB, String answerC, int correctAnswer){
this.description = description;
this.answerA = answerA;
this.answerB = answerB;
this.answerC = answerC;
this.correctAnswer = correctAnswer;
}

public Boolean isAnswerCorrect() {
return answerCorrect;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAnswerA() {
return answerA;
}
public void setAnswerA(String answerA) {
this.answerA = answerA;
}
public String getAnswerB() {
return answerB;
}
public void setAnswerB(String answerB) {
this.answerB = answerB;
}
public String getAnswerC() {
return answerC;
}
public void setAnswerC(String answerC) {
this.answerC = answerC;
}
public int getCorrectAnswer() {
return correctAnswer;
}
public void setCorrectAnswer(int correctAnswer) {
this.correctAnswer = correctAnswer;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}

驾驶测试.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DrivingTest {
int currentQuestionIndex = 0;
Question currentQuestion;
Boolean lastQuestion;
int score;
List<Question> q = new ArrayList<Question>();
Question quest = new Question();

public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);

while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();
}

public void setCurrentQuestionIndex(int currentQuestionIndex) {
this.currentQuestionIndex = currentQuestionIndex;
}
public int getCurrentQuestionIndex() {
return currentQuestionIndex;
}

public Boolean isLastQuestion() {
if(currentQuestionIndex == q.size() - 1){
return true;
}
else{
return false;
}
}

public Question getCurrentQuestion() {
return currentQuestion;
}

public void setCurrentQuestion(Question currentQuestion) {
this.currentQuestion = currentQuestion;
}

public int getScore() {
return score;
}
}

DrivingTestM.java(测试文件):

import java.io.FileNotFoundException;

public class DrivingTestMain {

public static void main( String args[] ) throws FileNotFoundException
{
DrivingTest drivingTest = new DrivingTest();

while( true )
{
// display the current question
Question question = drivingTest.getCurrentQuestion();
System.out.println( question.getDescription() );
System.out.println( "\t" + question.getAnswerA() );
System.out.println( "\t" + question.getAnswerB() );
System.out.println( "\t" + question.getAnswerC() + "\n" );

// set the answer to the current question to 1
drivingTest.getCurrentQuestion().setAnswer( 1 );

// if this is the last question, we are done.
if( drivingTest.isLastQuestion() ) break;

// it is not the last question, so increment CurrentQuestionIndex
int currentQuestionIndex = drivingTest.getCurrentQuestionIndex();
drivingTest.setCurrentQuestionIndex( currentQuestionIndex + 1 );
}

// display the test score
System.out.println( "Your test score is: " + drivingTest.getScore() );
}
}

最佳答案

在尝试使用当前问题之前,您没有在任何地方设置它。

你需要一个像这样的方法:

public void startTest()
{
currentQuestion = q.get(0);
}

然后:

   DrivingTest drivingTest = new DrivingTest();
drivingTest.startTest();

while( true )
{
//....

还要确保您有问题要解决,否则您还会收到其他错误..

如果您使用的是 eclipse,请尝试通过单步调试代码...

您可能还想寻找更好的方法来终止该循环,目前它会在测试结束时失败...

编辑:好吧,循环不会中断,但它真的很乱......

编辑 DrivingTest 构造函数:

public DrivingTest() throws FileNotFoundException{
File f = new File("DrivingTest.txt");
//int n = 1;
Scanner sc = new Scanner(f);

while(sc.hasNextLine()){
String desc = sc.nextLine();
String A = sc.nextLine();
String B = sc.nextLine();
String C = sc.nextLine();
String h = sc.nextLine();
int a = Integer.parseInt(h);
String blank = sc.nextLine();
q.add(new Question(desc, A,B,C,a) );
}
sc.close();

//ensure it's 0.
currentQuestionIndex = 0;
//sets up your first question object.
setCurrentQuestion( q.get(currentQuestionIndex) );
}

关于用于测试类的 Java Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26515553/

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