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 中的名称搜索直接进入默认的“找不到员工姓名”并且不允许用户输入。我查看了代码但找不到错误,有什么提示或帮助吗?

        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

3 回答 3

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()))

==操作员测试两个字符串是否是同一个对象(即字符串在内存中的相同位置)。

于 2012-11-19T12:12:49.757 回答
1
if (name == employee[k].getName())

将其更改为

if (name.equals(employee[k].getName()))
于 2012-11-19T12:13:50.760 回答
0

这是一个常见的问题。当您使用 nextInt() 读取整数值时,仅读取整数字符并将 \n 留在缓冲区中,然后当您调用 nextLine() 时,它不会提示用户输入,因为它已经有一个 ' \n'

为避免这种情况,在 showMenu 方法中执行

 console.nextLine();

在您使用 nextInt() 获得选择之后。

showMenu 的最后一部分是:

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

你应该使用 .equals() 方法来比较字符串,如下所示。

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

祝你好运 :)

于 2012-11-19T12:17:59.820 回答