簡體   English   中英

進行while循環的正確方法是什么? 我的方式不起作用

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

下面是我的程序,我的第一個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

(“您是否想產生更多標簽?是還是N”);

問題是從哪里開始的,我要問的是問用戶您想產生更多的標簽嗎? Y或N,如果它們將Y或y遮蓋起來,它將重復並使其更貼標簽。 為了做到這一點,我需要在while (enterAnother.equals("Y") || enterAnother.equals("y"))再進行一次while (count <= numBoxes)循環,謝謝您的幫助。 同樣,目前我的代碼沒有錯誤,但是當然,當我運行它時,一切都會大便。

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

您需要在循環結束時再次詢問:

while (enterAnother...){

..

enterAnother = JOptionPane.showInputDialog(...)
}

否則,enterAnother將永遠不會改變。

我看到了。 您需要在執行while循環之前將“ count”變量設置回0,以便在循環底部再次打印出標簽。 您還可以考慮將有關以下內容的問題轉給用戶:在循環打印出他們剛剛放入的內容后,他們是否要打印更多內容。我注意到,就像流程問題一樣,您問用戶要多少盒首先在第一輪,然后在循環中,您問用戶最后有多少個盒子。 就像用戶一樣,這讓我感到困惑。 只是給您一個心理上的注意。

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