1

我正在尝试创建一个测验,以便它询问您一个多项选择的问题,尽管我似乎无法弄清楚如何将其循环回不正确的问题。我对 java 编程仍然很陌生,不幸的是我的理解是不太好..

这是我目前编码的内容,

import java.util.*;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Quiz2
{
public static int question;
public static int question2;
public static int question3;
public static int question4;
public static int question5;

public static String guess;
public static String guess2;
public static String guess3;
public static String guess4;
public static String guess5;

public static void main(String[] args)
{   
    //create a scanner object
    Scanner sc = new Scanner(System.in);

    JOptionPane.showMessageDialog(null, "Hi, This is my Quiz\nYou will be asked a total of 5 questions in multiple choice format\nPlease answer as A/B/C/D (Not case sensative)", "Introduction",
    JOptionPane.INFORMATION_MESSAGE);

    //Asks for a paricular item from the keyboard
    guess  = JOptionPane.showInputDialog(null, "1. Which of the following is not a bright colour?\nA. Cyan\nB. Green\nC. Black\nD. Yellow\nAnswer: ", "Question 1",
    JOptionPane.INFORMATION_MESSAGE);

    //Asks for a paricular item from the keyboard
    guess2  = JOptionPane.showInputDialog(null, "2. What genre does the singer John Legend sing in?\nA. Pop\nB. R&b\nC. Rap\nD. Rock\nAnswer: ", "Question 2",
    JOptionPane.INFORMATION_MESSAGE);

    //Asks for a paricular item from the keyboard
    guess3  = JOptionPane.showInputDialog(null, "3. What is the largest discovered Star in the Universe?\nA. The Sun\nB. VY Canis Majoris\nC. Eta Carinae\nD. Beetlejuice\nAnswer: ", "Question 2",
    JOptionPane.INFORMATION_MESSAGE);

    //Asks for a paricular item from the keyboard
    guess4  = JOptionPane.showInputDialog(null, "4. Who is Ronaldinho Gaucho?\nA. A famous Soccer player\nB. A famous Singer\nC. The founder of Microsoft\nD. A famous Actor\nAnswer: ", "Question 2",
    JOptionPane.INFORMATION_MESSAGE);

    //Asks for a paricular item from the keyboard
    guess5  = JOptionPane.showInputDialog(null, "5. Who is the actor who played the role as the terminator in the movie Last Action Hero?\nA. Arnold Schwarzenegger\nB. Adam Sandler\nC. Jean Claude Van Damme\nD. Sylvester Stallone\nAnswer: ", "Question 2",
    JOptionPane.INFORMATION_MESSAGE);

    char x1 = guess.charAt(0);
    char x2 = guess2.charAt(0);
    char x3 = guess3.charAt(0);
    char x4 = guess4.charAt(0);
    char x5 = guess5.charAt(0);

    switch (x1)
    {
        case 'a': 
        case 'A':
                    JOptionPane.showMessageDialog(null, "Incorrect! Cyan is a bright colour, Please Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'b':
        case 'B':
                    JOptionPane.showMessageDialog(null, "Incorrect! Green is a bright colour, Please Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'c': 
        case 'C':
                    JOptionPane.showMessageDialog(null, "Correct! Black is not a bright colour", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'd':
        case 'D': 
                    JOptionPane.showMessageDialog(null, "Incorrect! Yellow is a bright colour, Please Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
    }



    switch (x2)
    {
        case 'a': 
        case 'A':
                    JOptionPane.showMessageDialog(null, "Incorrect! John Legend is not a Pop Singer!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'b':
        case 'B':
                    JOptionPane.showMessageDialog(null, "Correct! John Legend is a R&B Singer!", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'c': 
        case 'C':
                    JOptionPane.showMessageDialog(null, "Incorrect! John Legend does not Rap!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'd':
        case 'D': 
                    JOptionPane.showMessageDialog(null, "Incorrect! John Legend is not in the genre of Rock!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
    }

    switch (x3)
    {
        case 'a': 
        case 'A':
                    JOptionPane.showMessageDialog(null, "Incorrect! The Sun is the Biggest Star in our Solar System and not the Universe!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'b':
        case 'B':
                    JOptionPane.showMessageDialog(null, "Correct! VY Canis Majoris is currently the Largest Star Discovered in the Universe!", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'c': 
        case 'C':
                    JOptionPane.showMessageDialog(null, "Incorrect! Eta Carinae is the Second Largest Star Discovered in the Universe!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'd':
        case 'D': 
                    JOptionPane.showMessageDialog(null, "Incorrect! Beetlejuice is the Third Largest Star Discovered in the Universe!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
    }

    switch (x4)
    {
        case 'a': 
        case 'A':
                    JOptionPane.showMessageDialog(null, "Correct! Ronaldinho Gaucho is a famous Soccer player!", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'b':
        case 'B':
                    JOptionPane.showMessageDialog(null, "Incorrect! Ronaldinho Gaucho is not a Singer!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'c': 
        case 'C':
                    JOptionPane.showMessageDialog(null, "Incorrect! Bill Gates was the founder of Microsoft! NOT Ronaldinho Gaucho!!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'd':
        case 'D': 
                    JOptionPane.showMessageDialog(null, "Incorrect! Ronaldinho Gaucho is not an actor!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
    }

    switch (x5)
    {
        case 'a': 
        case 'A':
                    JOptionPane.showMessageDialog(null, "Incorrect! Arnold Schwarzenegger was the main actor of Last Action Hero, but in that same movie, who played the role as the terminator?\n Try Again!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'b':
        case 'B':
                    JOptionPane.showMessageDialog(null, "Incorrect! No its not Adam Sandler, but it would be funny if it was!,\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'c': 
        case 'C':
                    JOptionPane.showMessageDialog(null, "Incorrect! It could have been Jean Claude Van Damme!\nBut its not....\nPlease Try Again..", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
        case 'd':
        case 'D': 
                    JOptionPane.showMessageDialog(null, "Correct! Sylvester Stallone played the role as the Terminator in the Movie Last Action Hero!", "Answer CHECK!",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;
    }
}
}

现在,使用此代码..我如何为不正确的答案创建一个循环?帮助将不胜感激,谢谢。

4

4 回答 4

2

您需要将您的提问代码块提取到一个单独的方法(或 2 个!)中。如果需要,这将使您更方便地多次调用它。这看起来像:

while (!askQuestionsWithAllCorrect()) {
   System.out.println("Asking again...");
}

您当然可以让用户选择重新尝试 while 循环本身内的问题。

于 2012-09-01T13:35:10.543 回答
0

您可以使用各种循环机制(for、do-while 等)。程序何时(如果有的话)应该停止循环问题?它是否应该遍历所有问题(包括您正确回答的问题)?

简短的回答是您需要重复提问和检查问题。不需要重复定义问题。因此,您的循环(您决定使用哪个循环)应该围绕代码的一部分,即 (a) 显示问题 [char x1=...] 然后 (b) 检查并显示答案 [结束 switch 语句]。

于 2012-09-01T13:48:40.100 回答
0

一种方法是创建一个包含所有问题的地图。

Map<Integer, String> questionMap;

如果问题回答正确,请将其从地图中删除。您可以使用 a ConcurrentHashMap,它将支持以并发方式从地图中删除元素:

questionMap = new ConcurrentHashMap<Integer, String>();
// populate the map with your questions

for(Integer i : questionMap.keySet()) {
    if(t.isQuestionTrue(answer, questionMap.get(i))) {
        questionMap.remove(i);
    }
}

...或者您可以使用非并发映射并使用Iterator安全地同时删除元素。

questionMap = new HashMap<Integer, String>();
// populate the map with your questions

Iterator<Map.Entry<Integer, String>> it = questionMap.entrySet().iterator();
while(it.hasNext()) {
    Map.Entry entries = it.next();
    if(t.isQuestionTrue(answer, (String)entries.getValue())) {
        it.remove();
    }
}

然后将建立循环以在地图中剩余一个元素时继续运行。

while(questionMap.size() > 0) {
    // run through your program
}
于 2012-09-01T14:23:41.397 回答
0

您的方法仅适用于一个非常简单的示例,并且您显然无法对用户做出反应,因为您没有跟踪状态。

我写了这个做你想要的(我认为)。如果您对事情的方式/原因有任何疑问,请仔细查看并告诉我。

https://gist.github.com/3579413

(我把它放在一个公共要点上,因为有很多代码......)理想情况下,你会将这些类拆分到它们自己的文件中,这样你就不会得到一个非常麻烦的文件。希望你明白这一点。

于 2012-09-01T16:29:15.810 回答