2016-11-10 186 views
-2
class QuizMethods { 
    String [] questions = new String [20]; 
    String [] answers = new String [20]; 
    String [] correctAnswers = new String [20]; 
    int score; 
    void fillArrays() {  
     questions[0] = " Who is the lead guitarist of Led Zeppelin?"; 
     correctAnswers[0] = "Jimmy Page"; 

     questions[1] = "Who is the lead singer of Led Zeppelin?"; 
     correctAnswers[1]= "Robert Plant"; 

     questions[2] = "Who is the lead guitarist of Pink Floyd?"; 
     correctAnswers[2] = "David Gilmour"; 

     questions[3] = "Who is the bassist and main lyricist of Pink Floyd?"; 
     correctAnswers[3]= "Roger Waters"; 

     questions[4] = "Under which category of music are Pink Floyd entitled too?"; 
     correctAnswers[4] = "Soft Rock"; 

     questions[5] = "Under which category of music are Metallica entitled too?"; 
     correctAnswers[5]= "Trash Metal"; 

     questions[6] = "Who is the lead guitarist of Metallica?"; 
     correctAnswers[6] = "Kirk Hammet"; 

     questions[7] = "Who is the lead singer and rhythm guitarist of Metallica?"; 
     correctAnswers[7]= "James Hetfield"; 

     questions[8] = "Who is the lead guitarist of Guns n Roses?"; 
     correctAnswers[8] = "Slash"; 

     questions[9] = "Who is the lead singer of Guns n Roses?"; 
     correctAnswers[9]= "Axl Rose"; 

     questions[13] = "Under which category of music are Guns n Roses entitled too?"; 
     correctAnswers[13]= "Hard Rock"; 

     questions[11] = "Who is the bassist of Guns n Roses?"; 
     correctAnswers[11]= "Duff McKagan"; 

     questions[12] = "Name the biggest and most sold album of Pink Floyd"; 
     correctAnswers[12] = "The Wall"; 

     questions[13] = "Under which category of music are ZZ Top entitled too?"; 
     correctAnswers[13]= "Blues Rock"; 

     questions[14] = "Who is the lead guitarist and vocalist of ZZ Top?"; 
     correctAnswers[14] = "Billy Gibbons"; 

     questions[15] = "Who is considered as the king of blues?"; 
     correctAnswers[15]= "BB King"; 

     questions[16] = "Who was the lead singer for the popular band Queen?"; 
     correctAnswers[16] = "Freddie Mercury"; 

     questions[17] = "Who was the lead singer for the Heavy Metal band Black Sabbath?"; 
     correctAnswers[17]= "Ozzy Osbourne"; 

     questions[18] = "Who is the lead guitarist of Dire Straits?"; 
     correctAnswers[18] = "Mark Knopfler"; 

     questions[19] = "Complete the sentence.Alphaville released the song named _______ _______ in September 1984."; 
     correctAnswers[19]= "Forever Young"; 
    } 

    void takeQuiz(){ 
     String answer; 
     int i; 
     for (i = 0; i < 20; i++) { 
      System.out.print(questions[i] + "\nAnswer: "); 
      answer = Keyboard.readString(); 
      answers[i] = answer; 

      if (answer.equalsIgnoreCase(correctAnswers[i])) { 
       score++; 
      } else { 
       score--; 
      } 
     } 
    } 

    void quizResults() { 
    } 

    void performanceComment() { 
    } 

    void exit() { 
    } 

    void menu() { 
     System.out.println("1. Take Quiz"); 
     System.out.println("2. Quiz Results"); 
     System.out.println("3. Performance Comment"); 
     System.out.println("4. Exit"); 
     System.out.print("Choose from the above:"); 
     byte menu = Keyboard.readByte(); 
     switch(menu) { 

     case 1: 
      takeQuiz(); 
      break; 

     case 2: 
      quizResults(); 
      break; 

     case 3: 
      performanceComment(); 
      break; 

     case 4: 
      exit();   
     } 
    } 
} 

我想輸出的用戶的答案和正確答案的方法void quizResults()。我不知道用什麼來做到這一點。 如何在程序void exit()中結束程序?1.4.3用戶的答案和方法「無效quizResults()」正確答案

+1

請更具體地說明實際問題是什麼你遇到了。我們很樂意提供幫助,但是這很像您剛纔在這裏拋棄作業,並期待我們爲您解決問題。 – tnw

回答

0

你已經存儲了用戶的答案,並且你在數組中有正確的答案。 索引應該排隊,所以你可以循環訪問數組並輸出到屏幕。

喜歡的東西:

public void quizResults(){ 
    for(int i = 0; i<20; i++){ 
     System.out.println("Question: #" + i); 
     System.out.println("You answered: " + answers[i]); 
     System.out.println("Correct answer: " + correctAnswers[i]); 
} 

我也建議你把在默認情況下,switch語句。如果用戶輸入除1,2,3或4之外的其他值,則需要以適當的方式處理它(例如,表明他們輸入了無效選項的消息。)