2013-10-25 43 views
-1

我是一個開始的程序員,我試圖編寫一個簡單的程序,提出問題,然後以輸入「A」,「B」或「C」的形式提示您輸入該問題的答案對話框,但沒有出現對話框。其他一切似乎都很好。我如何製作它,讓我的測驗讓我回答?

這裏是我的代碼:

package homework; 

import javax.swing.JOptionPane; 

public class Quiz { 

    public static void main(String[] args) 
    { 


    int x = 0; 
    String[] quizQuestion = {"What is the color of the sky?", "What is the   
     color of the sea?", "What is the color of the earth?"}; 
    int score = 0; 
    String correct = "You are correct"; 
    String incorrect = "You are incorrect"; 
    String playerAnswerString = " "; 
    playerAnswerString.toUpperCase(); 
    char playerAnswer = playerAnswerString.charAt(0); 

    JOptionPane.showMessageDialog(null, "Test Your Knowledge!"); 
    JOptionPane.showMessageDialog(null, "Select an answer to the questions."); 
    for(x = 0; x < 3; x++) 
    { 
     JOptionPane.showMessageDialog(null, quizQuestion[x]); 
     while(quizQuestion.equals(0)) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 
      if(playerAnswer == 'A') 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
     while(quizQuestion.equals(1)) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 
      if(playerAnswer == 'B') 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
     while(quizQuestion.equals(2)) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 
      if(playerAnswer == 'C') 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
    } 
    JOptionPane.showMessageDialog(null, "You scored " + score + "/3."); 


} 
} 

在此先感謝。

爲清晰起見進行了編輯。

+0

你的問題是什麼?是不是按要求工作或顯示錯誤? – SpringLearner

+0

你用'while(quizQuestion.equals(0))'爲什麼? – Christian

+0

@javaBeginner,程序不會彈出輸入對話框來回答問題。 –

回答

1

一些修改完成後,請讓我知道代碼是否按您的期望工作。

import javax.swing.JOptionPane; 

public class Quiz { 

    public static void main(String[] args) 
    { 


    int x = 0; 
    String[] quizQuestion = {"What is the color of the sky?", "What is the  color of the sea?", "What is the color of the earth?"}; 
    int score = 0; 
    String correct = "You are correct"; 
    String incorrect = "You are incorrect"; 
    String playerAnswerString = " "; 
    playerAnswerString.toUpperCase(); 
    char playerAnswer = playerAnswerString.charAt(0); 

    JOptionPane.showMessageDialog(null, "Test Your Knowledge!"); 
    JOptionPane.showMessageDialog(null, "Select an answer to the questions."); 
    for(x = 0; x < 3; x++) 
    { 


     JOptionPane.showMessageDialog(null, quizQuestion[x]); 
     if(x==0) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 

      System.out.println(playerAnswerString+" "+playerAnswer); 

      if(playerAnswerString.equals("A")) 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
     if(x==1) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 
      if(playerAnswerString.equals("B")) 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
     if(x==2) 
     { 
      playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 
      if(playerAnswerString.equals("C")) 
      { 
       JOptionPane.showMessageDialog(null, correct); 
       score++; 
      } 
      else 
      { 
       JOptionPane.showMessageDialog(null, incorrect); 
      } 
     } 
    } 
    JOptionPane.showMessageDialog(null, "You scored " + score + "/3."); 


} 
} 
+0

工作。非常感謝你。 –

+0

歡迎@ChrisLong。有一件事我沒有在早期的代碼中理解是使用while循環(while(quizQuestion.equals(0)))。這個循環不斷重複問題,直到它沒有被正確回答。改變了這個邏輯.. –

+0

我相信我過去在想這件事,我對此仍然很陌生。我有不到一個月的經驗。 –

4
char playerAnswer = playerAnswerString.charAt(0); 

你必須分配playerAnswer的用戶後的值已經選擇在這裏他的答案

playerAnswerString = JOptionPane.showInputDialog(null, "A = Blue, B = Green, C = Brown"); 

否則變量playerAnswer將是空

+1

我希望這是你正在尋找的問題 –