好吧,所以我有一个while循环,当字符串的长度为0时执行,当字符串初始化为空字符串时,它适用于第一次传递,但在完成其余的这个循环嵌套的循环,它只是打破并以完全无意的方式工作。有人可以帮我查一下这个并帮我弄清楚什么是错的?
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));
}
}
}
并且输出最终是这样的:
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
如此处所示,循环在第一次循环之后的任何时间都无法正确执行。就好像name.length()值没有正确重置
答案 0 :(得分:0)
While (!check1.equals("9999"));
在重置值时,重置检查并检查3
check1 = "";
name = "";
check2=0;
check3=0;
age = 0;
salary = 0;
count++;
答案 1 :(得分:0)
我会继续说出来,因为您没有重置其他检查变量(check2
和check3
)。
因此,您的循环中将使用相同的检查值,但您仍然会在每个循环中重置年龄和薪水。
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
由于您似乎不想听取理由,因此我在Netbeans中对此进行了编译,并在每次迭代后输出check2
和check3
。也许这会告诉你这是你的问题:
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:
答案 2 :(得分:-1)
==测试参考相等。
.equals()测试值的相等性。
==在C#中工作