【发布时间】: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