有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如果回答不正确,java QandA程序循环A

我正在建立一个问答程序。但是我被卡住了,因为我想重复这个问题(如果用户在退出游戏之前还想继续回答这个问题,则不受限制)在回答正确之前,您无法移动到下一个。请帮帮我=

    String twoanswer = "Stamp";
    String tworesponse = "";
        tworesponse = JOptionPane.showInputDialog("It goes all over the world, but always stays in a corner. What is that?\nhint:_ _ _ _ _");
        if (tworesponse.equalsIgnoreCase(twoanswer))
        {
            JOptionPane.showMessageDialog(null, "You are correct!");
            JOptionPane.showMessageDialog(null, "Question Number: 3");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "You are wrong.");
            int reply1 = JOptionPane.showConfirmDialog(null, "Want to try again?", "Try again?", JOptionPane.YES_NO_OPTION);
                if (reply1 == JOptionPane.YES_OPTION)
                {
                    JOptionPane.showMessageDialog(null, "Answer again!");
                    //repeat to tworesponse ( the question)
                }
                if (reply1 == JOptionPane.NO_OPTION)
                {
                    int reply1dot1 = JOptionPane.showConfirmDialog(null, "Are you sure?", "Quit game?", JOptionPane.YES_OPTION);
                        if (reply1dot1 == JOptionPane.YES_OPTION)
                        {
                            JOptionPane.showMessageDialog(null, "Thank you for playing!");
                            System.exit(0);
                        }
                        if (reply1dot1 == JOptionPane.NO_OPTION)
                        {
                            int reply1dot2 = JOptionPane.showConfirmDialog(null, "Try again?", "Try again?", JOptionPane.YES_NO_OPTION);
                            if (reply1dot2 == JOptionPane.YES_OPTION)
                            {
                                JOptionPane.showMessageDialog(null, "Answer again!");
                                //return to tworesponse question
                            }
                            if (reply1dot2 == JOptionPane.NO_OPTION)
                            {
                                JOptionPane.showMessageDialog(null, "Thank you for playing!");
                                System.exit(0);
                            }
                        }
                }
        }
    }

}


共 (1) 个答案

  1. # 1 楼答案

    这是一种伪代码,您可以将其更改为java代码:

    for(Question question: questions){
        Answer answer = askQuestion(Question)
    
        if(question.answer = answer)
            showInfo("you are correct, next question will be " + question.num + 1)
        else{
            response = showInfo("you are wrong. do you want to try?")
    
            if(response = no)
                break;
        }
    
    }
    Answer askQuestion(Question question){
    
        return showQuestion(question);
    
    }