java分类查询

这段代码演示了如何根据员工类型(普通员工、程序员、设计师、架构师)进行数据统计,包括年龄、工资和收入的总和及平均值计算。通过switch-case语句将员工分配到对应的类别,并使用Count02类进行数据累加。最后输出各类员工的统计数据。
摘要由CSDN通过智能技术生成


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);

            }
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值