什么都没有从主要方法发布

时间:2013-12-17 07:45:46

标签: java arrays methods implementation main

当我从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());


}
}

4 个答案:

答案 0 :(得分:4)

此行中有错误

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++;
}

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

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

答案 1 :(得分:0)

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

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

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

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

这意味着您首先要输入一个int,然后输入以换行符结尾的内容然后输入答案。

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

questionHolder[i].answerCorrect(candidateAnswer)

questionHolder.answerCorrect(candidateAnswer)

答案 2 :(得分:0)

Quiz.giveQuiz()做的第一件事就是等待你的输入。因此,只要您在终端中输入内容并点击返回,它就不会输出任何内容。

可能要添加类似

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

这样用户(你;-))知道该怎么做。

答案 3 :(得分:0)

由于以下声明,上述代码无法编译:     if(questionHolder.answerCorrect(candidateAnswer))

应该是
    if(questionHolder [i] .answerCorrect(candidateAnswer))

并从

开始执行
 System.out.print(T1.giveQuiz());

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

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