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

时间:2015-11-12 19:29:30

标签: java loops while-loop infinite-loop

下面是我的程序和我的第一个同时法规工作正常,但第二个是我遇到问题的地方。在哪里说

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

下面是我的程序和我的第一个同时法规工作正常,但第二个是我遇到问题的地方。在哪里说

   enterAnother = JOptionPane.showInputDialog

("您想制作更多标签吗?Y或N");

是问题的开始我想要它做的是问用户你想生产更多的标签吗? Y或N,如果它们是Y或y,它会重复并使它们变得更加标签。为了实现这一点,我需要另一个(count <= numBoxes)循环 while (enterAnother.equals("Y") || enterAnother.equals("y"))任何帮助都表示赞赏,谢谢。此刻,我的代码没有错误,但当然,当我运行它时,它都会进入大便。

     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);
}

2 个答案:

答案 0 :(得分:1)

你需要在循环结束时再次询问:

while (enterAnother...){

..

enterAnother = JOptionPane.showInputDialog(...)
}

否则,enterAnother永远不会改变。

答案 1 :(得分:0)

我明白了。你需要设置&#34;计数&#34;在执行while循环之前将变量返回到0以在循环的底部再次打印出标签。您也可以考虑将问题转移给用户,以确定他们是否想要在您的while循环打印出他们刚刚放入的内容之后打印更多内容。我注意到,就像流程问题一样,您询问用户有多少个框首先在第一轮,然后在循环中,你问用户最后有多少个盒子。这让我很困惑,就像用户一样。只是给你一个心理记录。

    // 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;
    }