Используя JOptionPane.showMessageDialog, сообщите пользователю, сколько он получил правильно и неправильно - PullRequest
0 голосов
/ 06 ноября 2018

Следуйте комментариям в приведенном ниже коде: добавьте еще 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
             }
}

1 Ответ

0 голосов
/ 06 ноября 2018

Вот обновленная версия вашего кода:

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

Вам нужно изменить текст своих вопросов самостоятельно:).

...