简体   繁体   English

是什么导致我的while循环脱轨?

[英]What is causing my while loop to derail?

Alright, so I have a while loop that is made to execute while the length of a string is 0, and when the string is initialized as an empty string it works just fine for the first pass, but after completing the rest of the loop that this loop is nested in, it just breaks and works in a completely unintended manner. 好了,所以我有一个while循环,当字符串的长度为0时执行,并且当字符串初始化为空字符串时,它对于第一次通过就可以了,但是在完成循环的其余部分之后这个循环是嵌套的,它只是中断并以完全意外的方式工作。 Could somebody examine this for me and help me figure out whats wrong? 有人可以帮我检查一下,帮助我找出问题所在吗?

import java.util.*;
public class Project5
{
   public static void main(String[] args)
   {
       List namesLinkedList = new LinkedList();//creates a new linked list called namesLinkedList
       List ageLinkedList = new LinkedList();//creates a new linked list called ageLinkedList
       List salaryLinkedList = new LinkedList();//creates a new linked list called salaryLinkedList
       int length = loadLists(namesLinkedList, ageLinkedList, salaryLinkedList);//
       printLists(namesLinkedList, ageLinkedList, salaryLinkedList,length);
   }


    public static int loadLists(List namesLinkedList, List ageLinkedList, List salaryLinkedList)
    {
        Scanner input = new Scanner(System.in);
        String check1 = new String();
        int check2 = 0;
        int check3 = 0;
        String name = new String();
        int age = 0;
        int salary = 0;
        int count = 0;
        while(check1 != "9999")
       {
          while(name.equals(""))
          {

               System.out.print("Please enter an employee name: ");
               name = input.nextLine().trim();
               if(name.length() == 0)
               {
                 System.out.println("An employees name can't be an empty string."); 
               }
               else
               {
                   check1 = name;
               }
            }

           if(check1.equals("9999"))
           {
            break;   
           }

           while(check2 > 68 || check2 < 23)
           {
               System.out.print("Please enter an age for this employee: ");
               age = input.nextInt();
               if(age > 68 || age < 23)
               {
                   System.out.println("This age is not within the allowed range.( 23-68)");
               }
               else
               {
                   check2 = age;
               }
           }

           while(check3 > 120000 || check3 < 30000)
           {
               System.out.print("Please enter a salary for this employee: ");
               salary = input.nextInt();
               if(salary > 120000 || salary < 30000)
               {
                   System.out.println("This salary is not within the allowed range(30000-120000).");
               }
               else
               {
                   check3 = salary;
               }
           }
           namesLinkedList.add(name);
           ageLinkedList.add(age);
           salaryLinkedList.add(salary);
           check1 = "";
           name = "";
           age = 0;
           salary = 0;
           count++;
       }
        return count;
    }

    public static void printLists(List namesLinkedList, List ageLinkedList, List salaryLinkedList, int listLength)
    {
        System.out.printf("%-20s %-20s %14s\n", "Name","Age", "Salary");//prints a header
        for(int i = 0; i < listLength; i++)
        {
           System.out.printf("%-20s %-20s %14s\n", namesLinkedList.get(i),ageLinkedList.get(i),salaryLinkedList.get(i));
        }
    }
}

and the output ends up being something like this: 输出结果是这样的:

Please enter an employee name: Ace
Please enter an age for this employee: 12
This age is not within the allowed range.( 23-68)
Please enter an age for this employee: 34
Please enter a salary for this employee: 12000
This salary is not within the allowed range(30000-120000).
Please enter a salary for this employee: 45000
Please enter an employee name: An employees name can't be an empty string.
Please enter an employee name: Ellie
Please enter an employee name: Elise
Please enter an employee name: Alan
Please enter an employee name: 9999
Name                 Age                          Salary
Ace                  34                            45000
Ellier               0                                 0
Elise                0                                 0
Alan                 0                                 0

As shown here, the loop fails to execute properly any time after the first. 如此处所示,循环在第一个循环之后的任何时间都无法正确执行。 It's as if the name.length() value isnt properly resetting 好像name.length()值未正确重置

While (!check1.equals("9999"));

at the end while reseting values, reset check2 and check3 最后在重置值时,重置check2和check3

 check1 = "";
       name = "";
       check2=0;
       check3=0;
       age = 0;
       salary = 0;
       count++;

I'm going to go ahead and say it's because you aren't resetting your other check variables ( check2 and check3 ). 我将继续说这是因为您没有重置其他检查变量( check2check3 )。

Consequently, the same check values will be used in your loop, but you still reset age and salary on each loop. 因此,您的循环中将使用相同的检查值,但是您仍需在每个循环中重置年龄和薪水。

First loop
    Name loop
        check1 assigned as name is validation passes
    Age
        check2 assigned age if in range
    Salary
        check3 assigned if salary in range
    check1 = 0
    age = 0
    salary = 0
    check2 and check3 still equal previous age and salary
    leaving them valid values
Second loop
    Name loop
        check1 assigned (validation passing of course)
    Age loop
        check2 already assigned in range, next
    Salary loop
        check3 already assigned in range, next
Rinse, Repeat

Since you don't seem to want to listen to reason, I compiled this in Netbeans and output check2 and check3 after each iteration. 由于您似乎不想听原因,因此我在Netbeans中对此进行了编译,并在每次迭代后输出check2check3 Perhaps this will show you that this is your issue: 也许这将向您表明这是您的问题:

run:
Please enter an employee name: Ted
check2 currently equals 0
Please enter an age for this employee: 45
check3 currently equals 0
Please enter a salary for this employee: 67000
Please enter an employee name: An employees name can't be an empty string.
Please enter an employee name: Fred
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: Jane
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: Perry
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: 

== tests for reference equality. ==测试引用相等性。

.equals() tests for value equality. .equals()测试值是否相等。

== worked in C# ==在C#中工作

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

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