2016-12-07 16 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

, 난 당신이 추가 할 필요가 있다고 생각
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(); 
     } 
그것은 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(); 
      } 
+0

지금 질문을 표시하지만 답변도 표시됩니다. 어떻게 대답을 숨길 수 있습니까? –

+0

질문 클래스에서 toString() 메서드의 대답을 제거하십시오. – Sedrick

+0

좋아, 도와 줘서 고마워. –

1

I 정말로 내가 의견을 쓸 수 있으면 좋겠지 만 질문을 표시하는 코드는 보이지 않습니다. 사용자 입력을 어디에서 가져 왔는지 알 수 있습니다.

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

참고 : 나중에 다른 사람들과 함께 프로젝트를 진행하는 경우에는 가장 선호하는 텍스트를 추가하지 마십시오. 공간을 차지하며 추가 정보를 전달하지 않습니다. 특히 짧은 방법의 경우 시작 및 종료 태그를 추가 할 필요가 없습니다.

+0

질문은 내 드라이버 클래스 인 QuizTime에 있습니다. –

+0

여기서 질문을 작성하고 목록에 추가합니다. @sedrick이 말했듯이 for 루프 내부에'giveQuiz()'에 한 줄의 코드를 추가해야합니다. 그의 해결책은 당신의 문제를 바로 잡아야합니다. – Geoffrotism

+0

실제로 그랬습니다. 고맙습니다 –

관련 문제