2016-12-07 1 views
0

私は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

、私はあなたがscan.nextLine();


System.out.println(questionHolder[i].toString());
を追加する必要があると思います
 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(); 
     } 

それはこの方法ですべてのコードのように見えるがScanner scan = new Scanner(System.in);return getscore();、およびString candidateAnswer;

除いてforループの内側のようなものでなければなりません:

  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(); 
      } 
+0

今質問が表示されますが、回答も表示されます。どのように答えを隠すのですか? –

+0

あなたの質問クラスでは、toString()メソッドから答えを削除してください。 – Sedrick

+0

申し訳ありませんが、助けてくれてありがとうございます –

1

I本当に私がコメントすることができればいいと思っていますが、質問を表示しているコードは表示されません。私はあなたがユーザーの入力を取っている場所を参照してください。

for (int i = 0; i < numQuestions; i++) 
{ 
    candidateAnswer = scan.nextLine(); 
    if (questionHolder[i].answerCorrect(candidateAnswer)) 
    score++; 
} 

注:将来的には、あなたが他の人とのプロジェクトに取り組んでいる場合prettifyingテキストを追加することを控えるを試してみてください。それはスペースをとり、余分な情報を伝えません。特に、このような短い方法では、開始タグと終了タグを追加する必要はありません。

+0

質問は私のドライバクラス、QuizTimeにあります。 –

+0

質問を作成してリストに追加します。 @sedrickが述べたように、forループ内に 'giveQuiz()'のコード行を追加する必要があります。彼の解決策は、あなたの問題を修正する必要があります。 – Geoffrotism

+0

そうでした。ありがとうございました –

関連する問題