[英]Loop doesn't work as expected
我必须创建一个Java代码,为包装创建标签。 当用户输入“ y”或“ Y”以响应“您想创建更多”时,它将再次循环。 如果用户输入“ y”或“ Y”以外的任何内容,它将退出循环。 我遇到的问题是,在用户输入所有信息并被问到问题后,如果用户说了y或Y以外的任何内容,它确实会退出程序,但不会输出任何标签信息。 如果输入y或Y,它将正确输出标签信息,并再次完美地循环运行。 只是想知道是否有人可以帮助我了解我在做什么,以便我输入y或Y以外的任何内容后可以输出标签信息。
import javax.swing.JOptionPane;
public class MailOrderAFG //Define my public class
{
public static void main(String [] args)
{
String title, firstName, lastName, streetAddress, city, state, zip, numBoxesInput, enterAnother;
String a = "y", b = "Y";
title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
firstName = JOptionPane.showInputDialog("Enter first name: ");
lastName = JOptionPane.showInputDialog("Enter last name: ");
streetAddress = JOptionPane.showInputDialog("Enter street address: ");
city = JOptionPane.showInputDialog("Enter city: ");
state = JOptionPane.showInputDialog("Enter state: ");
zip = JOptionPane.showInputDialog("Enter zip code: ");
numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");
int numBoxes = 0;
int i;
while(enterAnother.equals(a) || enterAnother.equals(b)) //priming read
{
numBoxes = Integer.parseInt(numBoxesInput); //changing string to int so we can do arithmatic with it
{
for (i = 1; i <= numBoxes; i++) { //adding one until i is equal to numBoxes, then it will exit the loop
System.out.println(title + " " + firstName + " " + lastName);
System.out.println(streetAddress);
System.out.println(city + ", " + state + " " + zip);
System.out.println("Box " + i + " of " + numBoxes);
System.out.println();}
title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
firstName = JOptionPane.showInputDialog("Enter first name: ");
lastName = JOptionPane.showInputDialog("Enter last name: ");
streetAddress = JOptionPane.showInputDialog("Enter street address: ");
city = JOptionPane.showInputDialog("Enter city: ");
state = JOptionPane.showInputDialog("Enter state: ");
zip = JOptionPane.showInputDialog("Enter zip code: ");
numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");
}
}
System.exit(0);
}
}
我可以看到您通过尝试复制和粘贴原始代码创建了循环,但是生成的代码的工作流程不正确。
看,您的代码当前如下所示:
FETCH INFO FROM USER
ASK IF USER WANTS MORE
while (WANTSMORE)
PRINT INFO
FETCH INFO FROM USER
ASK IF USER WANTS MORE
end while
end program
看,它实际上应该就是这样:
do
FETCH INFO FROM USER
ASK IF USER WANTS MORE
PRINT INFO
while (WANTSMORE)
end program
查看是否可以重新排序代码以更正工作流程。 如果您需要有关do-while
信息, do-while
检查此链接 。
您的打印件位于循环中,在满足退出条件后该循环不会再次运行。
我建议您进行以下更改:
while-do
更改为“ do-while
。 或者,如果您需要知道用户是否已停止在其他地方输入:
boolean isRunning = true
变量。 这将是您的新循环退出条件。 y
,将退出条件设置为false
。 while
循环条件更改为while(isRunning)
与问题无关,但也很重要:
a
和b
。
main
类,表示程序的整个持续时间。 a
和b
这样a
变量名。 除了一些特殊情况(例如for-loop
索引)(对于i ndex,通常称为i
)外,您的变量应具有有意义的名称。 在这种情况下,不需要它们,但是它们将被命名为exitCharLowCase
和exitCharHighCase
。
JOptionPane
获取输入)。第二;对未来的良好做法)。 Objects
。
title
, firstName
, lastName
, //etc...
变量可以变成一个对象,使您可以处理所有这些变量,就好像它们只是带有“子”变量的一个变量一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.