我怎么做到这样我的测验让我回答?

时间:2013-10-25 06:47:59

标签: java loops joptionpane

我是一名初学程序员,我正在尝试编写一个简单的程序,询问一个问题,然后以“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.");


}
}

提前致谢。

为了清晰而编辑。

2 个答案:

答案 0 :(得分:5)

char playerAnswer = playerAnswerString.charAt(0);

你必须在用户选择答案后指定playerAnswer的值

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

否则变量playerAnswer将为空

答案 1 :(得分:2)

很少进行任何修改,请检查并告知我代码是否符合您的预期。

 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.");


}
}