我的下面的代码旨在为用户提供25个问题测验,并跟踪他们在最后打印出最终得分的正确数量。在这条线上
if (questionHolder.answerCorrect(candidateAnswer))
我收到一条错误消息,指出它无法在数组类型上调用answerCorrect
。如何修复此问题以便我的程序运行?我发布了下面的所有代码,但我的错误的类文件是Quiz.java谢谢!
//********************************************************************
// Question.java Author: Lewis/Loftus
//
// Represents a question (and its answer).
//********************************************************************
public class Question
{
private String question, answer;
//-----------------------------------------------------------------
// Constructor: Sets up the question with a default complexity.
//-----------------------------------------------------------------
public Question (String query, String result)
{
question = query;
answer = result;
}
//-----------------------------------------------------------------
// Returns the question.
//-----------------------------------------------------------------
public String getQuestion()
{
return question;
}
//-----------------------------------------------------------------
// Returns the answer to this question.
//-----------------------------------------------------------------
public String getAnswer()
{
return answer;
}
//-----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//-----------------------------------------------------------------
public boolean answerCorrect (String candidateAnswer)
{
return answer.equals(candidateAnswer);
}
//-----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//-----------------------------------------------------------------
public String toString()
{
return question + "\n" + answer;
}
}
import java.util.Scanner;
public class Quiz
{
private int score;
private Question[] questionHolder = new Question[25];
private int numQuestions;
public Quiz()
{
this.score = 0;
this.numQuestions = 0;
}
public void addQuestion (Question Q)
{
this.questionHolder[numQuestions++] = Q;
}
public int giveQuiz()
{
Scanner scan = new Scanner (System.in);
String candidateAnswer;
scan.nextInt();
scan.nextLine();
for (int i = 0; i < numQuestions; i++)
{
candidateAnswer = scan.nextLine();
if (questionHolder.answerCorrect(candidateAnswer))
score++;
}
return getscore();
}
public int getscore()
{
return score;
}
public String toString()
{
return getscore() + "\n";
}
}
public class QuizTime
{
public static void main (String[] args)
{
//--------------------------------------------------------------------------
//Initializes the variables.
//--------------------------------------------------------------------------
Question Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12, Q13, Q14, Q15, Q16, Q17, Q18, Q19,
Q20, Q21, Q22, Q23, Q24, Q25;
Quiz T1;
//--------------------------------------------------------------------------
//Creates the question and answer and also sets its complexity value.
//--------------------------------------------------------------------------
Q1 = new Question ("What is the capital of Virginia?", "Richmond");
Q2 = new Question ("Is an apple a Fruit or a vegetable?", "Fruit");
Q3 = new Question ("What continent is China in?", "Asia");
Q4 = new Question ("Is Germany in Europe or South America?", "Europe");
Q5 = new Question ("What color is a black bear?", "Black");
Q6 = new Question ("What is the capital of Arizona?", "Phoenix");
Q7 = new Question ("What do cows produce??", "Milk");
Q8 = new Question ("What ocean is closest to New York City?", "Atlantic");
Q9 = new Question ("What ocean surrounds Japan?", "Pacific");
Q10 = new Question ("What is the largest state in America?", "Alaska");
Q11 = new Question ("What is the smallest state?", "Deleware");
Q12 = new Question ("What is the most populated state?", "California");
Q13 = new Question ("What is instrument did Jascha Heifetz play?", "Violin");
Q14 = new Question ("Was Mozart a composer or a computer?", "Composer");
Q15 = new Question ("What is the largest country by area?", "Russia");
Q16 = new Question ("What is the most populated country?", "China");
Q17 = new Question ("What country did Pizza originate in?", "Italy");
Q18 = new Question ("What is the last name of the first American President?", "Washington");
Q19 = new Question ("What country borders America to the south?", "Mexico");
Q20 = new Question ("What island is 700 miles off the coast of NYC?", "Bermuda");
Q21 = new Question ("What city contains the Eiffel Tower?", "Paris");
Q22 = new Question ("Who wrote Romeo and Juliet?", "Shakespeare");
Q23 = new Question ("What swims in the ocean?", "Fish");
Q24 = new Question ("What is man's best friend?", "Dog");
Q25 = new Question ("What is another name for coffee and the language of this program?", "Java");
//--------------------------------------------------------------
//Adds the questions into quiz.
//--------------------------------------------------------------
T1= new Quiz();
T1.addQuestion(Q1);
T1.addQuestion(Q2);
T1.addQuestion(Q3);
T1.addQuestion(Q4);
T1.addQuestion(Q5);
T1.addQuestion(Q6);
T1.addQuestion(Q7);
T1.addQuestion(Q8);
T1.addQuestion(Q9);
T1.addQuestion(Q10);
T1.addQuestion(Q11);
T1.addQuestion(Q12);
T1.addQuestion(Q13);
T1.addQuestion(Q14);
T1.addQuestion(Q15);
T1.addQuestion(Q16);
T1.addQuestion(Q17);
T1.addQuestion(Q18);
T1.addQuestion(Q19);
T1.addQuestion(Q20);
T1.addQuestion(Q21);
T1.addQuestion(Q22);
T1.addQuestion(Q23);
T1.addQuestion(Q24);
T1.addQuestion(Q25);
//--------------------------------------------------------------
//Prints out the quizes.
//--------------------------------------------------------------
System.out.print(T1.giveQuiz());
}
}
答案 0 :(得分:2)
尝试这个
if (questionHolder[i].answerCorrect(candidateAnswer))
由于questionHolder
是一个数组。它不会有answerCorrect
API。因此,您必须从questionHolder
中选择问题。
答案 1 :(得分:1)
answerCorrect()
定义为Question
类型,questionHolder
定义为Question[]
更改
if (questionHolder.answerCorrect(candidateAnswer))
到
if (questionHolder[i].answerCorrect(candidateAnswer))
答案 2 :(得分:1)
您正在调用整个数组上的方法而不是当前的Question
对象。
将其替换为questionHolder[i].answerCorrect(candidateAnswer)
,它应该可以正常工作
答案 3 :(得分:1)
将该条件更改为
if (questionHolder[i].answerCorrect(candidateAnswer))