【问题标题】:Java - switch: case 2 and 3 do not functionJava - 开关:案例 2 和 3 不起作用
【发布时间】:2016-03-19 20:52:00
【问题描述】:

我想知道是否有人可以阐明为什么我的代码中的案例 2 和 3 似乎没有做任何事情,也许可以提供一些建议。我会提供我认为相关的任何信息,如果您要求更多详细信息,我会添加更多信息。

在发布我的代码之前,让我提供一些细节:我的程序被设计成一个非常简单的员工数据库。它使用 switch 语句在命令行上提供用户选项。开关的选项是:

  1. 将员工添加到数据库
  2. 列出数据库中的所有员工,并查看工资和工时
  3. 列出所有员工并显示公司福利(如果有)
  4. 终止程序

案例 1 将 Employee 对象添加到 Employee 类型的 ArrayList。 Employee 类负责跟踪员工姓名、工资、工作时间和他们工作的公司。

案例 1 和 4 似乎运行正常。

但是,案例 2 和 3 似乎没有做任何事情。这是开关的全部内容,它位于包含 main 方法的驱动程序类中:

        ArrayList<Employee> employees = new ArrayList<Employee>();

    int number = 0;

    while(number != 4)
    {

        System.out.print("Please select an option: " +
                "\n1) Add an Employee" +
                "\n2) List Employees" +
                "\n3) List Benefit Status" +
                "\n4) Quit"+ "\n");
        number = keyboard.nextInt();
        keyboard.nextLine();

        switch (number)
        {
            case 1:
                System.out.println("Hourly, contract, or salary employee? ");
                type = keyboard.nextLine();

                if(type.equalsIgnoreCase("hourly"))
                {
                    System.out.print("\nEnter the company: ");
                    comp = keyboard.nextLine();
                    System.out.print("\nEnter the first name: ");
                    fn = keyboard.nextLine();
                    System.out.print("\nEnter the last name: ");
                    ln = keyboard.nextLine();
                    System.out.print("\nEnter the hourly wage: ");
                    wage = keyboard.nextDouble();
                    System.out.print("\nEnter the hours worked: ");
                    hours = keyboard.nextInt();

                    Employee employee2 = new Employee(comp, fn, ln);
                    HourlyEmployee he = new HourlyEmployee(wage, hours);
                }

                else if(type.equalsIgnoreCase("contract"))
                {
                    System.out.print("\nEnter the company: ");
                                        comp = keyboard.nextLine();
                                        System.out.print("\nEnter the first name: ");
                                        fn = keyboard.nextLine();
                                        System.out.print("\nEnter the last name: ");
                                        ln = keyboard.nextLine();
                                        System.out.print("\nEnter the hourly wage: ");
                                        wage = keyboard.nextDouble();
                                        System.out.print("\nEnter the hours worked: ");
                                        hours = keyboard.nextInt();

                                        Employee employee2 = new Employee(comp, fn, ln);
                    ContractEmployee ce = new ContractEmployee(wage, hours);
                }

                else if (type.equalsIgnoreCase("salary"))
                {
                                        System.out.print("\nEnter the company: ");
                                        comp = keyboard.nextLine();
                                        System.out.print("\nEnter the first name: ");
                                        fn = keyboard.nextLine();
                                        System.out.print("\nEnter the last name: ");
                                        ln = keyboard.nextLine();
                                        System.out.print("\nEnter the salary: ");
                    salary = keyboard.nextDouble();

                    Employee employee2 = new Employee(comp, fn, ln);
                    SalaryEmployee se = new SalaryEmployee(salary);
                }

                else
                {
                    System.out.println("Invalid input.");
                    System.exit(0);
                }

                break;
            case 2:
                for(int i = 0; i < employees.size(); i++)
                {
                    System.out.println(employees.get(i).toString());
                }
                break;
            case 3:
                for(int i = 0; i < employees.size(); i++)
                {
                    System.out.println(employees.get(i).determineBenefits());
                }
                break;
            case 4:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid input.");
                System.exit(0);
        }
    }
}

}

在情况 2 和 3 中,我试图将 ArrayList 的索引作为参数分别传递给 toString() 方法和 determineBenefits() 方法。当这些方法与开关分开测试时,它们同样可以正常工作。这是 toString() 方法:

    public String toString()
{
    return firstName + " " + lastName + " from " + company +
            ". The worker's pay this week was $" + pay + ".";
}

还有determineBenefits()方法:

    public String determineBenefits()
{
    String benefits;

    if(isSalaryEmployee == true)
    {
        benefits = "This employee has a standard company health " +
            "insurance policy.";
    }

    else if(hhh  >= 40)
    {
        benefits = "This worker gets benefits.";
    }

    else
    {
        benefits = "No benefits.";
    }

    return benefits;
}

还有 Employee 构造函数,以防万一:

public Employee()
{

}

public Employee(String com, String first, String last)
{
    setCompany(com);
    setFirstName(first);
    setLastName(last);
}

那么,我应该如何传递位于 ArrayList 中的 Employee 对象?

【问题讨论】:

  • 您不会将Employee 对象添加到employees 列表的任何位置,因此当您打印列表的内容(案例2 和3)时,不会打印任何内容,因为列表是空的。跨度>
  • 旁注:您的代码过于复杂。不要只是逐行推动。考虑将事情分解成小方法。您不应该在循环内的开关内进行级联 if/else。这样的代码变得无法维护......也许在你写完之后 5 分钟
  • 感谢您指出这一点,Jesper。这就是问题所在,一个非常简单的遗漏错误很容易解决。标记为已解决。

标签: java arraylist switch-statement tostring


【解决方案1】:

正如 cmets 中所指出的,我的问题是我忘记将 Employee 对象实际添加到 ArrayList 中,这是一个非常容易修复的错误。感谢所有回答的人。

【讨论】:

    猜你喜欢
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多