jeudi 1 avril 2021

Java while loop help - nested if else statement

I am brand new to java and trying to create a story based around goldilocks and the three bears in which I ask the user a series of questions and the questions loop until the expected answer is given. It is for a class, and the expectation is we use while loops and if/ nested if else statements to do so. I have managed to get it set up so that the correct messages appear if the user inputs the wanted answer, however, haven't managed to get the question to loop if they input the wrong choice. Instead it just prints the error message and ends the program.

Can anyone give me any tips on where I am going wrong/ no exact fixes just a basic outline would be appreciated. Please assume that there is more to the program but I don't want to have anything flagged for plagiarism of myself later.

This is my if else statement so far (we are using GTerm https://jupiter.csit.rmit.edu.au/~e58140/GTerm/doc/GTerm.html hence gt.getInputString and so forth)

String porridge1;

porridge1 = gt.getInputString("How hot was the porridge? Cold, hot, somewhere in the middle?");
//dialog box asking user question about porridge
        gt.println("\n" + "How did Golidlocks find the porridge? Too cold, too hot, or just right?");
// printed message about porridge temperature for user to follow
        if (porridge1.equalsIgnoreCase("cold") || porridge1.equalsIgnoreCase(hot")) {
            gt.showErrorDialog("Oh no. This porridge was too " + porridge1 + "!");
//if user input = cold or hot; error dialog box appears
            gt.println("Oh no! This porridge was too " + porridge1 + "!");
//if user input = cold or hot; print error message
        } else {
            gt.showMessageDialog("This porridge is just the right temperature!");
//if user types anything else, dialog box confirming correct choice to appear
            gt.println("How wonderful! This porridge is just right!");
//if any other input given (middle, warm, perfect, just right etc, print following message


Aucun commentaire:

Enregistrer un commentaire