使用List Java时出错

时间:2013-10-16 03:01:08

标签: java list arraylist

尝试运行此代码时,我收到“找不到符号”错误。这是关于List代码行的。它将它扔在List和ArrayList上。我无法想象这是如何实现错误的。该脚本调用一个名为Employee的类。 List将包含从Employee创建的所有对象。然后应该能够打印List。

import java.util.Scanner;

class PayrollProgram
{


public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

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

    while (!emp.name.equals("STOP"))
    {
    Employee emp = new Employee();

    System.out.print("Employee's Name: ");
    emp.name = scan.next();

    if(emp.name.equals("STOP"))
        {
            System.out.printf("The Application is STOPPING......");
        break;
        }

    System.out.print("Enter hourly wage: $ ");
    emp.wage = scan.nextDouble();
    while (emp.wage < 0) 
    {
        System.out.printf("Please Enter a Positive Number! \n");
        System.out.print("Enter hourly wage: $ ");  
        emp.wage = scan.nextDouble();
    }

    System.out.print("Hours Worked in Week: ");
    emp.hours = scan.nextDouble();
    while (emp.hours < 0) 
    {
        System.out.printf("Please Enter a Positive Number! \n");
        System.out.print("Hours Worked in Week: ");
        emp.hours = scan.nextDouble();
    }

    employees.add(emp);
    emp.printEmployee();

    }
    for(Employee emp : employees)
        {
        System.out.println(emp.name);
        }
}

}

2 个答案:

答案 0 :(得分:5)

您既未导入java.util.List也未导入java.util.ArrayList。你应该这样做。

如果您已在单独的程序包中定义了Employee课程,则还需要导入该课程。

答案 1 :(得分:1)

你应该输入正确的包装和你也应该在while循环

之前创建员工的对象
Employee emp = new Employee();
while (!emp.name.equals("STOP"))
{

System.out.print("Employee's Name: ");
emp.name = scan.next();

if(emp.name.equals("STOP"))
    {
        System.out.printf("The Application is STOPPING......");
    break;
    }

System.out.print("Enter hourly wage: $ ");
emp.wage = scan.nextDouble();
while (emp.wage < 0) 
{
    System.out.printf("Please Enter a Positive Number! \n");
    System.out.print("Enter hourly wage: $ ");  
    emp.wage = scan.nextDouble();
}

System.out.print("Hours Worked in Week: ");
emp.hours = scan.nextDouble();
while (emp.hours < 0) 
{
    System.out.printf("Please Enter a Positive Number! \n");
    System.out.print("Hours Worked in Week: ");
    emp.hours = scan.nextDouble();
}

employees.add(emp);
emp.printEmployee();

}