2014-12-06 79 views
0

当我去添加员工时,此代码首先会提示输入新的员工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

如果帮助您解决问题,您应该考虑将答案标记为正确。 – SnakeDoc 2014-12-06 22:09:53

回答

2

它看起来像你要求用户输入一个id两次。

第一次是在这里:

id = scan.nextInt(); 
nothing = scan.nextLine(); 

第二次是在这里:

empNumber[x] = scan.nextInt(); 
scan.nextLine(); 

尝试

empNumber[x] = id; 

代替

empNumber[x] = scan.nextInt(); 
scan.nextLine(); 
+0

谢谢你的帮助 – 2014-12-06 22:08:32

相关问题