2012-11-19 37 views
1
//TestEmployeesProgram driver with menu & object array. 
import java.util.*; 
public class TestEmployeesProgram { 

public static Scanner console = new Scanner(System.in); 

public static void main(String[] args) { 

    final int MAX = 7; 

    Employee employee[] = new Employee[MAX]; 

    int choice,k; 
    String name; 
    boolean notFound; 

    employee[0] = new Manager("Jerry Bloggs","gfr",5,38.5); 
    employee[1] = new Manager("Joe Bloggs","gdr",4,32.5); 
    employee[2] = new Admin("Mary Jennings","nnv",35.3,88.5,34.3); 
    employee[3] = new Clerk("Brian Jones","bbl",42.4,78.5,23.5,45.3); 
    employee[4] = new Manager("John Bloggs","gvr",5,33.5); 
    employee[5] = new Admin("Bridget Jennings","nvv",45.3,98.5,36.3); 
    employee[6] = new Clerk("Jack Jones","bbb",43.4,78.5,23.5,47.3); 

    //Initial Read 
    choice = showMenu(); 

    //Continue Until 4/Exit 
    while (choice != MAX) { 

     switch (choice) { 
     case 1://Manager 

      System.out.println(); 
      System.out.printf("%s %-16s %-10s %6s","Name","Id","Hours Worked","Pay"); 
      System.out.println("\n=================================================="); 

      for (k = 0; k < MAX; ++k) 
      { 
       if (employee[k] instanceof Manager){ //use of string method instance of. 


        System.out.println(employee[k].toString()); 
       } 
      } 
      break; 

     case 2://Administration 

      System.out.println(); 
      System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota"); 
      System.out.println("\n=================================================="); 

      for (k = 0; k < MAX; ++k) 
      { 
       if (employee[k] instanceof Admin){ 
       System.out.println(employee[k].toString()); 

       } 
      } 
      break; 

     case 3://Clerk 

      System.out.println(); 
      System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold"); 
      System.out.println("\n=================================================="); 

      for (k = 0; k < MAX; ++k) 
      { 
       if (employee[k] instanceof Clerk){ 
       System.out.println(employee[k].toString()); 
       } 
      } 
      break; 

我正在運行的程序,並在案件4名搜索直接進入默認的「員工姓名未發現」和犯規讓用戶input.I通過代碼看,但不能找到錯誤,任何提示或幫助?Java的switch敘述,搜索陣列的

 case 4://Name search 

      System.out.print("Enter employee name: "); 
      name = console.nextLine(); 

      k = -1; 
      notFound = true; 



      while ((k < MAX-1) && (notFound)) 
      { 
       ++k; 
       if (name == employee[k].getName()){ 

        System.out.println(); 
        System.out.printf("%s %-16s %-10s %6s %-19s","Name","Id","Hours Worked","Pay","Admin Quota","Units Sold"); 
        System.out.println("\n=================================================="); 

        System.out.println(employee[k].toString()); 
        System.out.println(); 
        notFound = false; 
       } 


      }//end of case 4 while. 
      if (notFound){ 
       System.out.println("Employee name not found\n"); 
      } 
      break; 

     case 7://exit 
      System.out.println("Program exiting..."); 
      System.exit(0); 

     default: 
      System.out.println("Invalid menu choice 1..3 of 7 to Exit"); 



     }//end of switch 

     //sub read 
     choice = showMenu(); 

    }//end of while 






}//end of main 

//Menu method for employee selection. 
public static int showMenu() 
{ 

    int choice; 
    System.out.println(); 

    System.out.println("Employee Program Menu"); 

    System.out.println("1.Show Manager pay details "); 
    System.out.println("2.Show Admin pay details "); 
    System.out.println("3.Show Clerk pay details "); 
    System.out.println("4.Search by employee name "); 
    System.out.println("7.Exit"); 


    System.out.print("Enter option: "); 
    choice = console.nextInt(); 


    return choice; 
} 
} 

回答

4

有兩個錯誤。第一是在這裏:

System.out.print("Enter option: "); 
choice = console.nextInt(); 

nextInt方法不消耗結束行的字符。試試這個:

System.out.print("Enter option: "); 
String line = console.nextLine(); 
choice = Integer.parseInt(line); 

第二個錯誤是,在這裏,你應該使用的equals代替==比較字符串:

if (name == employee[k].getName()) 

試試這個:

if (name.equals(employee[k].getName())) 

==操作測試看看這兩個字符串是否是相同的對象(即字符串在內存中的相同位置)。

+0

恐怕這不回答這個問題,但是這是在他的代碼的問題之一。 –

+0

謝謝,我已經更新了答案。 –

1
if (name == employee[k].getName()) 

改變這

if (name.equals(employee[k].getName())) 
+0

不回答問題。 –

0

這是一個常見的問題。當您使用nextInt()讀取整數值時,只讀取整數字符,並且\ n留在緩衝區中,並且在此之後調用nextLine()時,它不會提示用戶輸入,因爲它已經得到' \ n」

你使用nextInt()的選擇後,爲了避免這種情況,在showMenu方法做

console.nextLine(); 

showMenu的最後這部分將是:

choice=console.nextInt(); 
console.nextLine(); 
return choice; 

,你應該使用.equals()方法如下比較字符串。

if (name.equals(employee[k].getName())) 

祝你好運:)

+0

感謝大家的支持:) –

+0

歡迎光臨:)哈哈這個qn是一個典型的例子,其中信用沒有去正確的人!無論如何..高興我們可以幫助:) –