简体   繁体   English

循环无法正常工作

[英]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: 我建议您进行以下更改:

  • Change from a while-do to a do-while . 从“ while-do更改为“ do-while

Or, if you need to know if the user has stopped input elsewhere: 或者,如果您需要知道用户是否已停止在其他地方输入:

  1. Add a boolean isRunning = true variable. 添加一个boolean isRunning = true变量。 This will be your new loop-exit condition. 这将是您的新循环退出条件。
  2. When the user answers y , set the exit condition to false . 当用户回答y ,将退出条件设置为false
  3. Change your while loops condition to simply be while(isRunning) 将您的while循环条件更改为while(isRunning)

Unrelated to the problem, but also important: 与问题无关,但也很重要:

  • Some of the variables you used, like a and b , are not needed. 您不需要使用某些变量,例如ab
    • Remember: Java will maintain variables while their context is active. 切记:Java将在其上下文处于活动状态时维护变量。 In this case, the context is the main class, meaning the whole duration of the program. 在这种情况下,上下文是main类,表示程序的整个持续时间。
    • While your current projects will be small and intended for learning, you will eventually be working on real code in big projects; 尽管您当前的项目很小,只适合学习,但最终您将在大型项目中从事真实代码的开发。 so it's a good idea to start developing good coding practice, instead having to overcome developed bad habits later. 因此,开始开发良好的编码习惯是一个好主意,而不必稍后克服已养成的不良习惯。
  • Avoid using variable names like a and b . 避免使用像ab这样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 . 在这种情况下,不需要它们,但是它们将被命名为exitCharLowCaseexitCharHighCase
  • Instead of repeating your feed code (the variables taking inputs from 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获取输入)。第二;对未来的良好做法)。
  • You're programming in an OO (Object Oriented) language. 您正在使用OO(面向对象)语言进行编程。 It's probably a little too soon yet, but if it doesn't overwhelm you, try to learn about OO and Objects in Java. 可能还为时过早,但是如果它没有使您不知所措,请尝试学习Java中的OO和Objects
    • Your 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. 您的titlefirstNamelastName//etc...变量可以变成一个对象,使您可以处理所有这些变量,就好像它们只是带有“子”变量的一个变量一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM