2013-10-25 2 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

거의 수정이 완료 비어 있습니다 여기에 그의 대답

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

를 선택했다 기대에 따라 코드가 작동하는지 확인하십시오.

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

나는 내가 그것을 생각하고 있다고 믿는다, 나는 아직도 이것에서 매우 새롭다. 나는 이것에 1 달도 채 못된 경험이있다. –

4
char playerAnswer = playerAnswerString.charAt(0); 

당신은 사용자 후 playerAnswer의 값을 할당해야 확인하고 나에게주세요, 그렇지 않으면 변수 playerAnswer이

+1

이것이 문제가되지 않았 으면 좋겠습니다. –

관련 문제