当我去添加员工时,此代码首先会提示输入新的员工ID。然后它检查数组以确保该ID尚未使用。我遇到的问题是,它让我输入ID两次,然后再转到其他的场地。其他字段只需输入一次。我无法弄清楚造成它的原因。你们能帮助我指出正确的方向吗?
示例输出:
Please enter the maximum number of employees for your store
2
Welcome to the employee management system
a) Add a new Employee
b) Delete an Employee
c) Print the Employee List
d) Print a Specific Employee
e) Exit the Employee Management System
a
Please enter the employee ID.
1234 //should only have to type it in once but as you can see in the example i have to type it a second time
//before it moves on
1234
Please enter the employee name.
我认为问题出在下面的方法中。
public void addEmployee() // choice a
{
int id = 0;
int x = 0;
int y = 0;
while (x < maxEmployees && empNumber[x] != 0) // allows user to add employee as long as x is less than maxEmployees and empNumber in next array is 0
x++;
if(x == maxEmployees)// tells user that they can not add more employees if they have too many before kicking them back to the menu
{
System.out.println("Unable to add more employees. Max number of employees reached.");
}
else
{
System.out.println("Please enter the employee ID.");
id = scan.nextInt();
scan.nextLine();
while (y < maxEmployees && empNumber[y] != id) // checks to see if employee id already exsist
y++;
if(y == maxEmployees) // if no matching id is found continues the adding process
{
empNumber[x] = scan.nextInt();
scan.nextLine();
System.out.println("Please enter the employee name.");
empName[x] = scan.nextLine();
System.out.println("Please enter the employee address.");
empAddress[x] = scan.nextLine();
System.out.println("Please enter the employee salary.");
empSalary[x] = scan.nextInt();
scan.nextLine();
}
else
System.out.println("This ID is already associated with an employee");
}
答案 0 :(得分:2)
看起来您要求用户输入两次ID。
第一次来了:
id = scan.nextInt();
nothing = scan.nextLine();
第二次来了:
empNumber[x] = scan.nextInt();
scan.nextLine();
尝试
empNumber[x] = id;
取代
empNumber[x] = scan.nextInt();
scan.nextLine();