简体   繁体   中英

What is the correct way to make a while loop?? my way is not working

Down below is my program and the my first while statment works just fine but the second on is where I ran into problems. Where is says

    enterAnother = JOptionPane.showInputDialog( " Do you want to produce more labels? Y or N " );
    while (enterAnother.equals("Y") || enterAnother.equals("y"))

Down below is my program and the my first while statment works just fine but the second on is where I ran into problems. Where is says

   enterAnother = JOptionPane.showInputDialog

( " Do you want to produce more labels? Y or N " );

is where the problems start what I want it to do is ask the user Do you want to produce more labels? Y or N and if they awnser Y or y it will repeat and make them more lables. In order to accomplish this do I need another while (count <= numBoxes) loop in the while (enterAnother.equals("Y") || enterAnother.equals("y")) any help at all is appreciated thank you. also at the moment my code is error free but of course when I run it it all goes to poop.

     import javax.swing.JOptionPane; // Imports JOptionPane class.

public class MailOrderEMHPractice
{
   public static void main( String[] args )
   {
// Declare string variables
String title;
String firstName;
String lastName;
String streetAddress;
String city;
String state;
String zip;
int numBoxes;
int count = 1;
String enterAnother = "Y"; //INITILIZE the loop control variable

String value = JOptionPane.showInputDialog("Enter Number of Boxes: ");
    numBoxes = Integer.parseInt(value);

//get input values from user
 title = JOptionPane.showInputDialog
( "What is your title ex. (Ms. Mr. Dr.) " );

//get input values from user
firstName = JOptionPane.showInputDialog
( "Enter First Name: " );

//get input values from user
lastName = JOptionPane.showInputDialog
( "Enter Last Name: " );

//get input values from user
streetAddress = JOptionPane.showInputDialog
( "Enter Street Address: " );

//get input values from user
city = JOptionPane.showInputDialog
( "Enter City: " );

//get input values from user
state = JOptionPane.showInputDialog
( "Enter State: " );

//get input values from user
zip = JOptionPane.showInputDialog
( "Enter Zip Code: " );


while (count <= numBoxes)
{
    System.out.println( "Box" + count + "of" + numBoxes);
    System.out.println( title + firstName + lastName );
    System.out.println( streetAddress );
    System.out.println( city + state + zip );
    count = count + 1;
}
//get input values from user
enterAnother = JOptionPane.showInputDialog
( " Do you want to produce more labels? Y or N " );

 while (enterAnother.equals("Y") || enterAnother.equals("y"))
 {
        //get input values from user
         title = JOptionPane.showInputDialog
        ( "What is your title ex. (Ms. Mr. Dr.) " );

        //get input values from user
        firstName = JOptionPane.showInputDialog
        ( "Enter First Name: " );

        //get input values from user
        lastName = JOptionPane.showInputDialog
        ( "Enter Last Name: " );

        //get input values from user
        streetAddress = JOptionPane.showInputDialog
        ( "Enter Street Address: " );

        //get input values from user
        city = JOptionPane.showInputDialog
        ( "Enter City: " );

        //get input values from user
        state = JOptionPane.showInputDialog
        ( "Enter State: " );

        //get input values from user
        zip = JOptionPane.showInputDialog
        ( "Enter Zip Code: " );

        String numBoxesString;
        numBoxesString = JOptionPane.showInputDialog
         ( "Enter Number of Boxes: " );
        numBoxes = Integer.parseInt(numBoxesString);

        //get input values from user to stop or continue loop
        enterAnother = JOptionPane.showInputDialog
        ( " Do you want to produce more labels? Y or N " );

        while (count <= numBoxes)
                        {
                            System.out.println( "Box" + count + "of" + numBoxes);
                            System.out.println( title + firstName + lastName );
                            System.out.println( streetAddress );
                            System.out.println( city + state + zip );
                            count = count + 1;
                        }

}
    // End program.
             System.exit(0);
}

You need to ask again at the end of your loop:

while (enterAnother...){

..

enterAnother = JOptionPane.showInputDialog(...)
}

Otherwise, enterAnother will never change.

I see it. You need to set your "count" variable back to 0 before you do the while loop to print out the labels again at the bottom of your loop. You may also consider moving your question to the user as to whether or not they want to print more to AFTER your while loop printing out what they just put in. I noticed, just as a flow issue, that you ask the user how many boxes first on the first round, and then in the loop, you ask the user how many boxes at the end. This confused me as it would a user. Just a mental note for you to make.

    // get input values from user to stop or continue loop
    enterAnother = JOptionPane
        .showInputDialog(" Do you want to produce more labels? Y or N ");
    count = 0; // <---- this is the code you need to put in to make it work
    while (count <= numBoxes)
    {
    System.out.println("Box" + count + "of"
        + numBoxes);
    System.out.println(title + firstName
        + lastName);
    System.out.println(streetAddress);
    System.out.println(city + state + zip);
    count = count + 1;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM