Search code examples
javajoptionpane

QandA program loop A if answered incorrectly


im currently building a question and answer program. but im stuck since i want to repeat the question (unlimited if the user still wants to continue answering the question until she/he quits the game.) you cant move to the next until answered correctly. please help me =.=

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

}


Solution

  • This is kind of pseudo code you can change it to java code:

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