簡體   English   中英

我如何做才能讓我的測驗讓我回答?

[英]How do I make it so my quiz lets me answer?

我是一個初學者,我正在嘗試編寫一個簡單的程序,先問一個問題,然后提示您在輸入對話框中輸入“ 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.");


}
}

提前致謝。

為清楚起見進行了編輯。

char playerAnswer = playerAnswerString.charAt(0);

用戶在此處選擇答案后,必須分配playerAnswer的值

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

否則變量playerAnswer將為空

所做的修改很少,請檢查並讓我知道代碼是否按您的期望工作。

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


}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM