第八章第四题(计算每个雇员每周工作的小时数)(Calculate the number of hours each employee works per week)

第八章第四题(计算每个雇员每周工作的小时数)(Calculate the number of hours each employee works per week)

  • **8.4(计算每个雇员每周工作的小时数)假定所有雇员每周工作的小时数存储在一个二维数组中,。1行包含7列,记录了一个雇员7天的工作小时数。例如:右边数组存储了8个雇员的工作小时数。编写一个程序,按照总工时降序的方式显示雇员和他们的总工时。
    在这里插入图片描述

*8.4(Calculate the number of hours each employee works per week)Assume that all employees work hours per week in a two-dimensional array,. One row contains seven columns, recording the number of hours an employee worked in seven days. For example, the array on the right stores the working hours of eight employees. Write a program that displays employees and their total hours in descending order.
在这里插入图片描述

  • 参考代码:

    package chapter08;
    
    public class Code_04 {
        public static void main( String[] args ){
            int[][] time = {
                    {2,4,3,4,5,8,8},
                    {7,3,4,3,3,4,4},
                    {3,3,4,3,3,2,2},
                    {9,3,4,7,3,4,1},
                    {3,5,4,3,6,3,8},
                    {3,4,4,6,3,4,4},
                    {3,7,4,8,3,8,4},
                    {6,3,5,9,2,7,9}
            };
            int[] employee = new int[8];
            int[] flag = new int[8];
            for (int i = 0;i < 8;i++) {
                for (int j = 0; j < 7; j++) {
                    employee[i] += time[i][j];
                }
                flag[i] = i;
            }
            for (int i = 0;i < 7;i++){
                int temp;
                for (int j = 0;j < 7 - i;j++){
                    if (employee[j] > employee[j + 1]){
                        temp = employee[j];
                        employee[j] = employee[j + 1];
                        employee[j + 1] = temp;
                        temp = flag[j];
                        flag[j] = flag[j + 1];
                        flag[j + 1] = temp;
                    }
                }
            }
            for (int i = 0;i < 8;i++){
                System.out.println("Employee" + flag[i] + "'s total time is " + employee[i]);
            }
        }
    }
    
    
  • 结果显示:

    Employee2's total time is 20
    Employee1's total time is 28
    Employee5's total time is 28
    Employee3's total time is 31
    Employee4's total time is 32
    Employee0's total time is 34
    Employee6's total time is 37
    Employee7's total time is 41
    
    Process finished with exit code 0
    
    
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值