【问题标题】:What is the correct way to make a while loop?? my way is not working制作while循环的正确方法是什么?我的方法行不通
【发布时间】:2016-02-14 06:21:03
【问题描述】:

下面是我的程序,我的第一个 while 语句工作得很好,但第二个是我遇到问题的地方。在哪里说

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

下面是我的程序,我的第一个 while 语句工作得很好,但第二个是我遇到问题的地方。在哪里说

   enterAnother = JOptionPane.showInputDialog

("你想生产更多的标签吗?Y还是N");

问题出在哪里我想要它做的是问用户你想生产更多的标签吗? Y 或 N,如果他们遮住 Y 或 y,它将重复并使它们成为更多标签。为了做到这一点,我需要另一个 while (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);
}

【问题讨论】:

  • ...当我运行它时,一切都变成了便便。也许你可以更具体。

标签: java loops while-loop infinite-loop


【解决方案1】:

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

while (enterAnother...){

..

enterAnother = JOptionPane.showInputDialog(...)
}

否则,enterAnother 永远不会改变。

【讨论】:

  • 我稍微更新了我的代码,现在它要求输入,然后会再次提出问题,如果你对问题回答否,它会结束,但它不会出现新标签。
【解决方案2】:

我看到了。在执行 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;
    }

【讨论】:

  • 是的,这就是问题所在。我想我的代码现在已经完成了,谢谢!!
  • 不客气。如果您愿意,请不要忘记将我的答案标记为已接受并投票。 :) 谢谢!
猜你喜欢
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多