简体   繁体   English

进行while循环的正确方法是什么? 我的方式不起作用

[英]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. 下面是我的程序,我的第一个while陈述很好用,但是第二个是我遇到问题的地方。 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. 下面是我的程序,我的第一个while陈述很好用,但是第二个是我遇到问题的地方。 Where is says 哪里说

   enterAnother = JOptionPane.showInputDialog

( " Do you want to produce more labels? Y or N " ); (“您是否想产生更多标签?是还是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. Y或N,如果它们将Y或y遮盖起来,它将重复并使其更贴标签。 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. 为了做到这一点,我需要在while (enterAnother.equals("Y") || enterAnother.equals("y"))再进行一次while (count <= numBoxes)循环,谢谢您的帮助。 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. 否则,enterAnother将永远不会改变。

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. 您需要在执行while循环之前将“ count”变量设置回0,以便在循环底部再次打印出标签。 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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