没有从主方法启动任何内容



当我从3个类文件启动下面的程序时,什么都不会发生。它只是显示一个空控制台。我是不是错过了某种实现?它由3个类文件组成,我没有来自编译器的错误消息。该程序只需显示每个测试问题,玩家回答,然后在最后获得最终分数。谢谢

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());

}
}

此行中有错误

if (questionHolder.answerCorrect(candidateAnswer)) // questionHolder is an array

由于questionHolder是一个数组,因此需要在循环中提供索引。

if (questionHolder[i].answerCorrect(candidateAnswer)) // like this, [i] - index

此外,程序启动,但等待来自用户的输入。没有SOP来指示用户输入值,因此会让您认为它什么都不做。

// Waiting for the user to input an integer value, but there is no SOP to intimate the 
// user about it, thus making it seem like doing nothing.
scan.nextInt();
scan.nextLine();

要向用户提问,请在for循环中打印。

for (int i = 0; i < numQuestions; i++) {
    System.out.println(questionHolder[i].getQuestion()); // Question is displayed to the user now. Answer accordingly.
    candidateAnswer = scan.nextLine();
    if (questionHolder[i].answerCorrect(candidateAnswer))
        score++;
}

记住这一点,我觉得giveQuiz()for循环之前的这两行是不必要的。

// scan.nextInt();
// scan.nextLine();

它为您提供了一个空控制台,因为当您调用以下行时

System.out.print(T1.giveQuiz());

它首先执行giveQuizz()开关,等待来自Scanner的输入。

scan.nextInt();
scan.nextLine();

这意味着您必须首先键入int,然后键入以换行符结尾的内容,然后键入答案。

我希望这只是一个复制粘贴错误,但访问questionHolder的正确版本方式是

questionHolder[i].answerCorrect(candidateAnswer)

不是

questionHolder.answerCorrect(candidateAnswer)

Quiz.giveQuiz()要做的第一件事就是等待您的输入。所以它不会输出任何东西,直到你在终端中键入一些东西并点击回车键。

你可能想添加之类的东西

 System.out.println("Please type a string and and integer and hit return");

通过这种方式,用户(您;-))知道该做什么。

由于以下语句,上面的代码没有编译:if(questionHolder.answerCorrect(candidateAnswer))

应该是
如果(问题持有者[i].answer正确(候选答案))

并且从开始执行

 System.out.print(T1.giveQuiz());

当执行开始时,您必须输入值

你没有得到任何显示,因为你既没有提示任何地方接受答案,也可能输入那么多值,循环结束后你就会得到分数。

相关内容

  • 没有找到相关文章

最新更新