JAVA员工名字 年龄 工资 工种

2022-11-17,,,

如题:

下面是我个人的写法

输出部分使用了 格式化输出 有兴趣的朋友可以了解一下:

解决的思路大致为: 创建一个对象数组--> 数组下标为0的数组中张三这个变量对应 String name;

25对应age 1000对应工资 3为工种。

其中:

st[i].work(st[i].job); 为 将对象输入的值赋值给下面work方法中int job的值.
 public static void main(String[] args) {
YuanGong[] st = {
new YuanGong("张三",25,1000,3),
new YuanGong("李四",28,2500,2),
new YuanGong("小明",22,1000,3),
new YuanGong("小红",23,2500,2),
new YuanGong("赵四",35,3500,1)
};
for (int i = 0;i<st.length;i++){
int sum = 0; // System.out.printf("姓名:"+st[i].name+" 年龄:"+st[i].age+" 工资:"+st[i].gongzi+" 工作内容:");
System.out.printf("姓名:%s 年龄:%d 工资:%.2f 工作内容:",st[i].name,st[i].age,st[i].gongzi);
st[i].work(st[i].job);
}
int sum = 0;
for (int a=0;a<st.length;a++){
//平均工资
sum+=st[a].gongzi;
}
System.out.println("平均工资为:"+sum/st.length);
}
}
class YuanGong {
String name;
int age;
double gongzi;
int job; public YuanGong() {
} public YuanGong(String name, int age, double gongzi, int job) {
this.name = name;
this.age = age;
this.gongzi = gongzi;
this.job = job;
}
public void work(int job){
if (job==1){
System.out.println("在办公室喝茶");
}else if (job==2){
System.out.println("在外跑业务");
}else if (job==3){
System.out.println("在厂房工作");
}else {
System.out.println("工种有误");
}
}

JAVA员工名字 年龄 工资 工种的相关教程结束。

《JAVA员工名字 年龄 工资 工种.doc》

下载本文的Word格式文档,以方便收藏与打印。