简体   繁体   中英

Loop doesn't work as expected

I have to create a java code that creates labels for packages. When the user inputs "y" or "Y" in response to "Would you like to create more" it will go through the loop again. If the user enters anything other than "y" or "Y", it will exit the loop. The problem I'm having is that after a user inputs all the information and is asked the question, if the user says anything other than y or Y it does exit the program, but it doesn't output any of the label information. If I enter y or Y, it outputs the label information correctly and runs through the loop again perfectly. Just wondering if anyone can help me see what I'm doing wrong so that I can get it to output the label info after I enter anything other than y or Y.

import javax.swing.JOptionPane;

public class MailOrderAFG //Define my public class
{
    public static void main(String [] args)
    {
    String title, firstName, lastName, streetAddress, city, state, zip, numBoxesInput, enterAnother;
    String a = "y", b = "Y";
    title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
    firstName = JOptionPane.showInputDialog("Enter first name: ");
    lastName = JOptionPane.showInputDialog("Enter last name: ");
    streetAddress = JOptionPane.showInputDialog("Enter street address: ");
    city = JOptionPane.showInputDialog("Enter city: ");
    state = JOptionPane.showInputDialog("Enter state: ");
    zip = JOptionPane.showInputDialog("Enter zip code: ");
    numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
    enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");
    int numBoxes = 0;
    int i;


    while(enterAnother.equals(a) || enterAnother.equals(b)) //priming read

    {



        numBoxes = Integer.parseInt(numBoxesInput); //changing string to int so we can do arithmatic with it
        {


            for (i = 1; i <= numBoxes; i++) { //adding one until i is equal to numBoxes, then it will exit the loop
            System.out.println(title + " " + firstName + " " + lastName);
            System.out.println(streetAddress);
            System.out.println(city + ", " + state + " " + zip);
            System.out.println("Box " + i + " of " + numBoxes);
            System.out.println();}



            title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
            firstName = JOptionPane.showInputDialog("Enter first name: ");
            lastName = JOptionPane.showInputDialog("Enter last name: ");
            streetAddress = JOptionPane.showInputDialog("Enter street address: ");
            city = JOptionPane.showInputDialog("Enter city: ");
            state = JOptionPane.showInputDialog("Enter state: ");
            zip = JOptionPane.showInputDialog("Enter zip code: ");
            numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
            enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");

        }

    }

        System.exit(0);

}
}

I can see you created the loop by trying to copy and paste the original code, but the resulting code's workflow isn't correct.

See, your code is currently like this:

FETCH INFO FROM USER
ASK IF USER WANTS MORE

while (WANTSMORE)
    PRINT INFO

    FETCH INFO FROM USER
    ASK IF USER WANTS MORE
end while

end program

See, it should actually be simply this:

do
    FETCH INFO FROM USER
    ASK IF USER WANTS MORE
    PRINT INFO
while (WANTSMORE)

end program

See if you can reorder your code in order for the workflow to be corrected. Check this link if you need info on do-while .

Your prints are inside of your loop, which doesn't run again after the exit condition is met.

I suggest you make the following changes:

  • Change from a while-do to a do-while .

Or, if you need to know if the user has stopped input elsewhere:

  1. Add a boolean isRunning = true variable. This will be your new loop-exit condition.
  2. When the user answers y , set the exit condition to false .
  3. Change your while loops condition to simply be while(isRunning)

Unrelated to the problem, but also important:

  • Some of the variables you used, like a and b , are not needed.
    • Remember: Java will maintain variables while their context is active. In this case, the context is the main class, meaning the whole duration of the program.
    • While your current projects will be small and intended for learning, you will eventually be working on real code in big projects; so it's a good idea to start developing good coding practice, instead having to overcome developed bad habits later.
  • Avoid using variable names like a and b . Except for a few specific cases like in for-loop indexes (usually called i , for i ndex), your variables should have meaningful names. In this case, they aren't needed, but they would be named something like exitCharLowCase and exitCharHighCase .
  • Instead of repeating your feed code (the variables taking inputs from JOptionPane ) twice, you could have it in a function, and call that function twice instead, simplifying your code, and reducing maintenance work (only has to find and change one code instead of two; good practice for future).
  • You're programming in an OO (Object Oriented) language. It's probably a little too soon yet, but if it doesn't overwhelm you, try to learn about OO and Objects in Java.
    • Your title , firstName , lastName , //etc... variables can be turned into an object, allowing you to deal with all those variables as if they were just one variable with "child" variables.

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