使用java编写程序实现输入员工工资,获得员工的平均工资,要求使用象数组类型的
答案:1 悬赏:50 手机版
解决时间 2021-11-10 02:42
- 提问者网友:萌卜娃娃
- 2021-11-09 13:34
使用java编写程序实现输入员工工资,获得员工的平均工资,要求使用象数组类型的
最佳答案
- 五星知识达人网友:有你哪都是故乡
- 2021-11-09 13:45
一:将员工姓名、工资封装成一个对象
public class Staff {
private String name;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Staff(String name, int salary) {
super();
this.name = name;
this.salary = salary;
}
}
二:初始化一个数组,算平均工资
public class Average {
public static void main(String[] args) {
Staff staffs[] = {new Staff("zhangsan", 1000), new Staff("lisi", 1100), new Staff("wangwu", 1200)};
int sum = 0;
for(Staff staff : staffs) {
sum = sum + staff.getSalary();
}
System.out.println("员工人数:" + staffs.length + " 总工资:" + sum + " 平均工资:" + sum / staffs.length);
}
}
public class Staff {
private String name;
private int salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Staff(String name, int salary) {
super();
this.name = name;
this.salary = salary;
}
}
二:初始化一个数组,算平均工资
public class Average {
public static void main(String[] args) {
Staff staffs[] = {new Staff("zhangsan", 1000), new Staff("lisi", 1100), new Staff("wangwu", 1200)};
int sum = 0;
for(Staff staff : staffs) {
sum = sum + staff.getSalary();
}
System.out.println("员工人数:" + staffs.length + " 总工资:" + sum + " 平均工资:" + sum / staffs.length);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯