第八章第四题(计算每个雇员每周工作的小时数)(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