jeudi 22 septembre 2016

While loop is affecting code requiring the same joption pane to be repeated twice in java

I had to install a while loop that prevents strings or any letter to be accept into my first joptionpane. My code works exactly how I want it to work but I had to repeat the same joptionpane right blow the while loop to get my code to work. All I want to do is delete the line int amount= Integer.parseInt(JOptionPane.showInputDialog( "How many Guest are coming?:"));. I just want the line in the while loop is that possible?

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


    boolean error; //by default false
    do{
        try{
            int 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);




int amount= Integer.parseInt(JOptionPane.showInputDialog( "How many Guest are coming?:")); //[SECOND TIME]


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

     int e;
     double cc;
     double pc;
     double sax= 17.95;
     double tnt;
     double o;
     double d= 1.06;
     double vla=2.03;
     double strawc = 3.47;






 if (ageCheck) {
     String gender =  JOptionPane.showInputDialog( "enter flavor");








     boolean isCSV = gender.matches("[CcSsVv]");
     if (!isCSV) {
         // It isn't C, c, S, s, V or v

          JOptionPane.showMessageDialog(null, "Your did not enter a valid number");
     }



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

            e = 2;

        pc = ((amount/2.0) );
        o =Math.ceil(pc);
        tnt = o *sax;
        cc = (amount*d);


    boolean genderCheck2 = gender.equalsIgnoreCase("v");

            e = 2;

        pc = ((amount/2.0) );
        o =Math.ceil(pc);
        tnt = o *sax;
        cc = (amount*vla);

     boolean genderCheck3 = gender.equalsIgnoreCase("s");

     e = 2;

     pc = ((amount/2.0) );
     o =Math.ceil(pc);
     tnt = o *sax;
     cc = (amount*strawc);







 if (genderCheck1) {

     JOptionPane.showMessageDialog(
        null, "The Cost of the Room is : " + e + " ICE COST: "+tnt+"MORE COST" +cc+" TOTAL COST" + (e+tnt+cc+45));}


 if (genderCheck2) {

     JOptionPane.showMessageDialog(
        null, "The Cost Peer Room is : " + e + " ICE COST: "+tnt+"MORE COST" +cc+" TOTAL COST" + (e+tnt+cc));}

     if (genderCheck3) {

         JOptionPane.showMessageDialog(
                                       null, "The Cost Peer Room is : " + e + " ICE COST: "+tnt+"MORE COST" +cc+" TOTAL COST" + (e+tnt+cc));}}



















}}

Aucun commentaire:

Enregistrer un commentaire