gpt4 book ai didi

java - 创建 while 循环的正确方法是什么?我的方法行不通

转载 作者:行者123 更新时间:2023-12-01 10:54:43 25 4
gpt4 key购买 nike

下面是我的程序,我的第一个 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);
}

最佳答案

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

while (enterAnother...){

..

enterAnother = JOptionPane.showInputDialog(...)
}

否则,enterAnother 将永远不会改变。

关于java - 创建 while 循环的正确方法是什么?我的方法行不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com