class Count02 { int sum_age; int sum_wages; int sum_income; int count; public void add(Employees02 e, int i) { sum_age += Integer.parseInt(e.employees[i][3]); sum_wages += Integer.parseInt(e.employees[i][4]); sum_income += Integer.parseInt(e.employees[i][4]); count++; if (e.employees[i].length > 5) { sum_income += Integer.parseInt(e.employees[i][5]); if (e.employees[i].length > 6) { sum_income += Integer.parseInt(e.employees[i][6]); } } } } class Employees02 { String[][] employees = { {"10", "1", "段誉", "22", "3000"}, {"13", "2", "令狐冲", "32", "18000", "15000", "2000"}, {"11", "3", "任我行", "23", "7000"}, {"11", "4", "张三丰", "24", "7300"}, {"12", "5", "周芷若", "28", "10000", "5000"}, {"11", "6", "赵敏", "22", "6800"}, {"12", "7", "张无忌", "29", "10800", "5200"}, {"13", "8", "韦小宝", "30", "19800", "15000", "2500"}, {"12", "9", "杨过", "26", "9800", "5500"}, {"11", "10", "小龙女", "21", "6600"}, {"11", "11", "郭靖", "25", "7100"}, {"12", "12", "黄蓉", "27", "9600", "4800"} }; } public class Task02 { public static void main(String[] args) { //创建一个员工对象 Employees02 e2 = new Employees02(); // 为每一类员工创建一个对象 Count02 ord_emp = new Count02(); Count02 programmer = new Count02(); Count02 designer = new Count02(); Count02 architect = new Count02(); // 循环遍历员工的数据 for (int i = 0; i < e2.employees.length; i++) { // i 从0-11 switch (e2.employees[i][0]) { case "10": // 普通员工 e.employees[i] 这一条数据就是普通员工 // 当 i = 0时 e.employees[i] = {"10 1 段誉 22 3000} ord_emp.add(e2, i); break; case "11"://程序员 programmer.add(e2, i); break; case "12"://设计师 designer.add(e2, i); break; case "13"://架构师 architect.add(e2, i); break; } } Count02[] c = {ord_emp, programmer, designer, architect}; String[] s = {"普通员工", "程序员", "设计师", "架构师"}; for (int i = 0; i < c.length; i++) { System.out.println("=============="+s[i]+"=================="); double avg_age=(double)(c[i].sum_age/c[i].count); double avg_wages=(double)(c[i].sum_wages/c[i].count); //打印输出结果 System.out.println("总年龄:"+c[i].sum_age); System.out.println("总收入:"+c[i].sum_income); System.out.println("总工资:"+c[i].sum_wages); System.out.println("总人数:"+c[i].count); System.out.println("平均年龄:"+avg_age); System.out.println("平均工资:"+avg_wages); } } }
java分类查询
最新推荐文章于 2024-07-21 22:27:40 发布
这段代码演示了如何根据员工类型(普通员工、程序员、设计师、架构师)进行数据统计,包括年龄、工资和收入的总和及平均值计算。通过switch-case语句将员工分配到对应的类别,并使用Count02类进行数据累加。最后输出各类员工的统计数据。
摘要由CSDN通过智能技术生成