【问题标题】:Using JOptionPane.showMessageDialog tell the user how many they got right and wrong使用 JOptionPane.showMessageDialog 告诉用户他们有多少正确和错误
【发布时间】:2018-11-06 00:38:39
【问题描述】:

按照下面代码中的 cmets:再添加 8 个问题和答案,最后告诉用户他们得到了多少正确和错误的分数。

import javax.swing.*;

public class Quiz
{
       public static void main(String[] args)

       {
              String[] questions = 
                       {"How many players on a basketball team?\nA. 5 B. 6 C. 7",
                                      "How many points for a basket?\nA. 1 B. 2 C. 3",
                        "How many points for a free throw?\nA. 1 B.     2 C. 3"};
              //Add 8 more questions and answers
              char[] answers = {'A', 'B','A' };
              char ans = ' ';
              int x, correct = 0;
              String entry;
              boolean isGood;
              for(x = 0; x < questions.length; ++x)
              {
                         isGood = false;
                         int firstError = 0;
                         while(!isGood)
                         {
                                isGood = true;
                                entry = JOptionPane.showInputDialog
           (null,questions[x]);
                                ans = entry.charAt(0);
                                if(ans != 'A' && ans != 'B' && ans != 'C')
                                {
                                       isGood = false;
                                       if(firstError == 0)
                                       {
            questions[x] = questions[x] +
            "\nYour answer must be A, B or C.";
                                                firstError = 1;
                                        }
                                }
                           }
                           if(ans == answers[x])
                           {
                                   ++correct;
             JOptionPane.showMessageDialog(null,
             "Correct!");
                            }
                            else
             JOptionPane.showMessageDialog(null, "The correct answer is " + answers[x]);
                    }
                    // Using JOptionPane.showMessageDialog tell the user how many they got right and wrong
             }
}

【问题讨论】:

  • 所以,您一直在提供一些代码并被要求对其进行修改,然而,您没有付出任何努力,而是将其发布在这里,甚至没有提出任何问题?这不是 SO 的工作方式——你的努力将得到回报。在哪里 - 具体来说你被困在哪里?你有什么累的,怎么不适合你?
  • 是的,我不确定您对这个网站的了解,但它不是“我们将为您做作业的网站”。这是一个为专业(和爱好者)程序员提供的帮助彼此解决问题的网站。但我们也愿意帮助学生(以及一般的初学者),如果他们表现出一些努力首先尝试自己解决问题。所以试一试,如果有什么具体的事情让你卡住了,然后回来向我们解释那是什么。祝你好运,欢迎访问该网站。

标签: java swing joptionpane


【解决方案1】:

这是您的代码的更新版本:

import javax.swing.*;

public class Quiz
{
       public static void main(String[] args)

       {
              String[] questions = 
                       {"How many players on a basketball team?\nA. 5 B. 6 C. 7",
                        "How many points for a basket?         \nA. 1 B. 2 C. 3",
                        "How many points for a free throw?     \nA. 1 B. 2 C. 3",
                        "Question 4?                           \nA. 1 B. 2 C. 3",
                        "Question 5?                           \nA. 1 B. 2 C. 3",
                        "Question 6?                           \nA. 1 B. 2 C. 3",
                        "Question 7?                           \nA. 1 B. 2 C. 3",
                        "Question 8?                           \nA. 1 B. 2 C. 3",
                        "Question 9?                           \nA. 1 B. 2 C. 3",
                        "Question 10?                          \nA. 1 B. 2 C. 3",
                        "Question 11?                          \nA. 1 B. 2 C. 3"};
              //Add 8 more questions and answers
              char[] answers = {'A', 'B', 'A', 'C', 'C', 'A', 'B', 'A', 'A', 'C', 'B'};
              char ans = ' ';
              int x, correct = 0;
              String entry;
              boolean isGood;
              for(x = 0; x < questions.length; ++x)
              {
                         isGood = false;
                         int firstError = 0;
                         while(!isGood)
                         {
                                isGood = true;
                                entry = JOptionPane.showInputDialog (null,questions[x]);
                                ans = entry.charAt(0);
                                if(ans != 'A' && ans != 'B' && ans != 'C')
                                {
                                       isGood = false;
                                       if(firstError == 0)
                                       {
                                           questions[x] = questions[x] + "\nYour answer must be A, B or C.";
                                           firstError = 1;
                                        }
                                }
                           }
                           if(ans == answers[x])
                           {
                                ++correct;
                                JOptionPane.showMessageDialog(null, "Correct!");
                            }
                            else
                                JOptionPane.showMessageDialog(null, "The correct answer is " + answers[x]);
                    }
                    JOptionPane.showMessageDialog(null, "You got " + correct + " correct answers and " + (answers.length-correct) + " wrong answers.");
             }
}

您需要自己修改问题的文本:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2013-12-31
    相关资源
    最近更新 更多