2014-12-06 97 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

相關問題