vendredi 23 septembre 2016

How to put a while loop into another while loop in java

The code below is basically a party planner calculator. One question is supposed to ask how many people are coming to the party. The other question asked is what flavor ice cream will be served. I am trying to get one while loop inserted into another while loop. I would like 2 while loops to do the following things. The first while loop will validate how many guest are coming; if a user does not enter a number between 1 and 19 a error message would come saying please state a valid number. The 2nd while loop I would like to validate the flavor of the ice cream. If the user does not enter c; I would like a error message to come up. I have put my code below.

import javax.swing.JOptionPane;
public class x{
public static void main(String[] args) {

    int amount=0;
    boolean error;
    do {
        try {
            amount = Integer.parseInt(JOptionPane.showInputDialog("How many Guest are coming?:"));//[FIRST TIME]
            error = false; //successful so it will exit from the loop
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Your did not enter a valid number");
            error = true;
        }
    } while (error);


    boolean ageCheck = (amount >= 1) && (amount <= 19);


    int e;
    double cc;
    double pc;


    if (ageCheck) {

        boolean error1;
        do {
        try {
            String gender = JOptionPane.showInputDialog("enter flavor");


                boolean genderCheck1 = gender.equalsIgnoreCase("c");

                e = 2;

                pc = ((amount / 2.0));



                if (genderCheck1) {

                    JOptionPane.showMessageDialog(
                                                  null, "The Cost of the Room is : " + e + " ICE COST: " +pc);
                }

                     error1 = false; //successful so it will exit from the loop
                } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Your did not enter a valid numberx");
                error1 = true;
            }
        } while (error1);

    }

}}

Aucun commentaire:

Enregistrer un commentaire