[英]Loop doesn't work as expected
I have to create a java code that creates labels for packages. 我必须创建一个Java代码,为包装创建标签。 When the user inputs "y" or "Y" in response to "Would you like to create more" it will go through the loop again.
当用户输入“ y”或“ Y”以响应“您想创建更多”时,它将再次循环。 If the user enters anything other than "y" or "Y", it will exit the loop.
如果用户输入“ y”或“ Y”以外的任何内容,它将退出循环。 The problem I'm having is that after a user inputs all the information and is asked the question, if the user says anything other than y or Y it does exit the program, but it doesn't output any of the label information.
我遇到的问题是,在用户输入所有信息并被问到问题后,如果用户说了y或Y以外的任何内容,它确实会退出程序,但不会输出任何标签信息。 If I enter y or Y, it outputs the label information correctly and runs through the loop again perfectly.
如果输入y或Y,它将正确输出标签信息,并再次完美地循环运行。 Just wondering if anyone can help me see what I'm doing wrong so that I can get it to output the label info after I enter anything other than y or 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);
}
}
I can see you created the loop by trying to copy and paste the original code, but the resulting code's workflow isn't correct. 我可以看到您通过尝试复制和粘贴原始代码创建了循环,但是生成的代码的工作流程不正确。
See, your code is currently like this: 看,您的代码当前如下所示:
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
See, it should actually be simply this: 看,它实际上应该就是这样:
do
FETCH INFO FROM USER
ASK IF USER WANTS MORE
PRINT INFO
while (WANTSMORE)
end program
See if you can reorder your code in order for the workflow to be corrected. 查看是否可以重新排序代码以更正工作流程。 Check this link if you need info on
do-while
. 如果您需要有关
do-while
信息, do-while
检查此链接 。
Your prints are inside of your loop, which doesn't run again after the exit condition is met. 您的打印件位于循环中,在满足退出条件后该循环不会再次运行。
I suggest you make the following changes: 我建议您进行以下更改:
while-do
to a do-while
. while-do
更改为“ do-while
。 Or, if you need to know if the user has stopped input elsewhere: 或者,如果您需要知道用户是否已停止在其他地方输入:
boolean isRunning = true
variable. boolean isRunning = true
变量。 This will be your new loop-exit condition. y
, set the exit condition to false
. y
,将退出条件设置为false
。 while
loops condition to simply be while(isRunning)
while
循环条件更改为while(isRunning)
Unrelated to the problem, but also important: 与问题无关,但也很重要:
a
and b
, are not needed. a
和b
。
main
class, meaning the whole duration of the program. main
类,表示程序的整个持续时间。 a
and b
. a
和b
这样a
变量名。 Except for a few specific cases like in for-loop
indexes (usually called i
, for i ndex), your variables should have meaningful names. for-loop
索引)(对于i ndex,通常称为i
)外,您的变量应具有有意义的名称。 In this case, they aren't needed, but they would be named something like exitCharLowCase
and exitCharHighCase
. exitCharLowCase
和exitCharHighCase
。
JOptionPane
) twice, you could have it in a function, and call that function twice instead, simplifying your code, and reducing maintenance work (only has to find and change one code instead of two; good practice for future). JOptionPane
获取输入)。第二;对未来的良好做法)。 Objects
in Java. Objects
。
title
, firstName
, lastName
, //etc...
variables can be turned into an object, allowing you to deal with all those variables as if they were just one variable with "child" variables. title
, firstName
, lastName
, //etc...
变量可以变成一个对象,使您可以处理所有这些变量,就好像它们只是带有“子”变量的一个变量一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.