请问哪里出错了/

请问哪里出错了/

import java.util.List;
import java.util.ArrayList;
public class EmployeeTest {
    public static void main(String[] args) {
		//定义ArrayList对象
        ArrayList clerkList=new ArrayList();
        //创建三个Employee类的对象
        Employee clerk1=new Employee("张三",5000.0);
        Employee clerk2=new Employee("李四",5500.0);
        Employee clerk3=new Employee("赵六",4000.0);
        //添加员工信息到ArrayList中
        clerkList.add(clerk1);
        clerkList.add(clerk2);
        clerkList.add(clerk3);
        //显示员工的姓名和薪资
        System.out.println("员工姓名 员工薪资");
        for(int i=0;i<clerkList.size();i++){
            System.out.println((Employee)(clerkList.get(i)).getName()+"  "+((Employee)(clerkList.get(i)).getSalary()));
        }
	}
}
public class Employee{
 //根据需求完成Employee类的定义  
    private String name;
    private double salary;
    public Employee(){
        
    }
    public Employee(String name,double salary){
        this.setName(name);
        this.setSalary(salary);
    }
    
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return this.name;
    }
    public void setSalary(double salary){
       this.salary=salary;
    }
    public double getSalary(){
        return this.salary;
    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
一叶知秋519 2017-10-22 22:58:00

你好,这个输出的for循环,你可以对照一下:

 System.out.println("员工姓名 员工薪资");
        for(int i=0;i<clerkList.size();i++){
            System.out.println(((Employee) clerkList.get(i)).getName()+"  "+((Employee) clerkList.get(i)).getSalary());
        }

祝学习愉快~

提问者 Homo_sapiensQ 2017-10-21 09:42:23

上面有错修改了还是不对,如下

import java.util.List;
import java.util.ArrayList;
public class EmployeeTest {
    public static void main(String[] args) {
		//定义ArrayList对象
        ArrayList clerkList=new ArrayList();
        //创建三个Employee类的对象
        Employee clerk1=new Employee("张三",5000.0);
        Employee clerk2=new Employee("李四",5500.0);
        Employee clerk3=new Employee("赵六",4000.0);
        //添加员工信息到ArrayList中
        clerkList.add(clerk1);
        clerkList.add(clerk2);
        clerkList.add(clerk3);
        //显示员工的姓名和薪资
        System.out.println("员工姓名 员工薪资");
        for(int i=0;i<clerkList.size();i++){
            System.out.println((Employee)(clerkList.get(i)).getName()+"  "+(Employee)(clerkList.get(i)).getSalary());
        }
	}
}

请问哪里有问题?

提示信息

/7/4108252/110/2061/EmployeeTest.java:18: error: cannot find symbol
           System.out.println((Employee)(clerkList.get(i)).getName()+"  "+(Employee)(clerkList.get(i)).getSalary());
                                                          ^
 symbol:   method getName()
 location: class Object
/7/4108252/110/2061/EmployeeTest.java:18: error: cannot find symbol
           System.out.println((Employee)(clerkList.get(i)).getName()+"  "+(Employee)(clerkList.get(i)).getSalary());
                                                                                                      ^
 symbol:   method getSalary()
 location: class Object
Note: /7/4108252/110/2061/EmployeeTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

  • System.out.println((Employee)(clerkList.get(i)).getName()+" "+(Employee)(clerkList.get(i)).getSalary());这一句里面的(Employee)(clerkList.get(i)).getName()前面把(Employee)(clerkList.get(i))再用括号括起来
    2017-11-24 17:20:45
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师