所以我创建了一个包含25个问题的测验,但是当我运行我的驱动程序课程时,问题没有显示出来。只有用户输入答案的框。我该怎么做才能解决这个问题?
public class Question implements Complexity // defines the class as Question, implements the interface Complexity
{ // begins class
private String question; // sets question as a String that is only accessible in certain areas
private String answer; // sets answer as a String that is only accessible in certain areas
private int complexityLevel; // sets complexityLevel as an int to determine the complexity level, only accessible in certain areas
//----------------------------------------------------------------
// Sets up the question with a default complexity.
//----------------------------------------------------------------
public Question (String query, String result) // sets Question as a constructor to create a question and sets complexitylevel to 1
{ // begins block
question = query; // sets question equal to query
answer = result; // sets answer equal to result
complexityLevel = 1; // sets the default complexity level to 1
} // ends block
//----------------------------------------------------------------
// Sets the complexity level for this question.
//----------------------------------------------------------------
public void setComplexity (int level) // sets setComplexity as a public void to set the complexitylevel of a question
{ // begins block
complexityLevel = level; // sets complexityLevel equal to level
} // ends block
//----------------------------------------------------------------
// Returns the complexity level for this question.
//----------------------------------------------------------------
public int getComplexity() // sets getComplexity as an int to return the complexity level
{ // begins block
return complexityLevel; // returns the complexity level
} // ends block
//----------------------------------------------------------------
// Returns the question.
//----------------------------------------------------------------
public String getQuestion() // set getQuestion as a public String to return the question
{ // begins block
return question; // returns the question
} // ends block
//----------------------------------------------------------------
// Returns the answer to this question.
//----------------------------------------------------------------
public String getAnswer() // sets getAnswer as a public String to return the answer to the question
{ // begins block
return answer; // returns the answer
} // ends block
//----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//----------------------------------------------------------------
public boolean answerCorrect (String candidateAnswer) // sets answerCorrect as a public boolean to return true if the user's answer equals the question
{ // begins block
return answer.equals(candidateAnswer); // returns true
} // ends block
//----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//----------------------------------------------------------------
public String toString() // sets toString as a String to return the question and answer as a String
{ // begins block
return question + "\n" + answer; // returns the question and answer as a String
} // ends block
} // ends class
测验课:
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.nextLine();
for (int i = 0; i < numQuestions; i++)
{
candidateAnswer = scan.nextLine();
if (questionHolder[i].answerCorrect(candidateAnswer))
score++;
}
return getscore();
}
public int getscore()
{
return score;
}
public String toString()
{
return getscore() + "\n";
}
}
驱动程序类:
public class QuizTime
{
//--------------------------------------------------------------------------
// Creates the question and answer.
//--------------------------------------------------------------------------
public static void main (String[] args)
{
Quiz T1;
Question Q1 = new Question ("What is the capital of Virginia?", "Richmond");
Question Q2 = new Question ("Is an apple a Fruit or a vegetable?", "Fruit");
Question Q3 = new Question ("What continent is China in?", "Asia");
Question Q4 = new Question ("Is Germany in Europe or South America?", "Europe");
Question Q5 = new Question ("What color is a black bear?", "Black");
Question Q6 = new Question ("What is the capital of Arizona?", "Phoenix");
Question Q7 = new Question ("What do cows produce??", "Milk");
Question Q8 = new Question ("What ocean is closest to New York City?", "Atlantic");
Question Q9 = new Question ("What ocean surrounds Japan?", "Pacific");
Question Q10 = new Question ("What is the largest state in America?", "Alaska");
Question Q11 = new Question ("What is the smallest state?", "Deleware");
Question Q12 = new Question ("What is the most populated state?", "California");
Question Q13 = new Question ("What is instrument did Jascha Heifetz play?", "Violin");
Question Q14 = new Question ("Was Mozart a composer or a computer?", "Composer");
Question Q15 = new Question ("What is the largest country by area?", "Russia");
Question Q16 = new Question ("What is the most populated country?", "China");
Question Q17 = new Question ("What country did Pizza originate in?", "Italy");
Question Q18 = new Question ("What is the last name of the first American President?", "Washington");
Question Q19 = new Question ("What country borders America to the south?", "Mexico");
Question Q20 = new Question ("What island is 700 miles off the coast of NYC?", "Bermuda");
Question Q21 = new Question ("What city contains the Eiffel Tower?", "Paris");
Question Q22 = new Question ("Who wrote Romeo and Juliet?", "Shakespeare");
Question Q23 = new Question ("What swims in the ocean?", "Fish");
Question Q24 = new Question ("What is man's best friend?", "Dog");
Question 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 quiz.
//--------------------------------------------------------------
System.out.println(T1.giveQuiz());
}
}
答案 0 :(得分:1)
我真的希望我能发表评论,但是我没有看到你在显示问题的代码。我看到你在用户输入的位置。
for (int i = 0; i < numQuestions; i++)
{
candidateAnswer = scan.nextLine();
if (questionHolder[i].answerCorrect(candidateAnswer))
score++;
}
注意:将来,如果您正在与他人合作,请尝试避免添加美化文本。它占用空间并且不传达任何额外信息。特别是对于这种简短的方法,您不需要添加开始和结束标记。
答案 1 :(得分:0)
在此代码中,我认为您需要添加
System.out.println(questionHolder[i].toString());
在scan.nextLine();
之前
public int giveQuiz()
{
Scanner scan = new Scanner (System.in);
String candidateAnswer;
scan.nextLine();
for (int i = 0; i < numQuestions; i++)
{
candidateAnswer = scan.nextLine();
if (questionHolder[i].answerCorrect(candidateAnswer))
score++;
}
return getscore();
}
看起来此方法中的所有代码都应位于for循环内,Scanner scan = new Scanner(System.in);
,return getscore();
和String candidateAnswer;
类似的东西:
public int giveQuiz()
{
Scanner scan = new Scanner (System.in);
String candidateAnswer;
for (int i = 0; i < numQuestions; i++)
{
System.out.println(questionHolder[i].toString());
candidateAnswer = scan.nextLine();
if (questionHolder[i].answerCorrect(candidateAnswer))
score++;
}
return getscore();
}