/*
result:-----------------------------------------------
sony:java sony$ java EmployeeHours < EmployeeHours.txt
Employee1: 41
Employee2: 37
Employee3: 34
Employee4: 32
Employee5: 31
Employee6: 28
Employee7: 28
Employee8: 20
EmployeeHours.txt------------------------------------
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
*/
import java.util.Scanner;
public class EmployeeHoursSortInsert {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int COLUMN = 7;
final int ROW = 8;
int[][] hours = new int[ROW][COLUMN];
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++)
hours[i][j] = input.nextInt();
}
int[] sum = sumOfHours(hours);
sortDescend(sum);
showEmployee(sum);
}
public static int[] sumOfHours(int[][] h) {
int[] sum = new int[h.length];
for (int i = 0; i < h.length; i++) {
for (int j = 0; j < h[i].length; j++)
sum[i] += h[i][j];
}
return sum;
}
public static void sortDescend(int[] s) {
for (int i = 1, j; i < s.length; i++) {
int currentElement = s[i];
for (j = i - 1; j >= 0 && s[j] < currentElement; j--) {
s[j+1] = s[j];
}
s[j+1] = currentElement;
}
}
public static void showEmployee(int[] h) {
for (int i = 0; i < h.length; i++)
System.out.println("Employee" + (i + 1) + ": " + h[i]);
}
}
Introduction to Java Programming编程题7.4<计算每个雇员每周工作小时数>
最新推荐文章于 2022-03-09 18:28:57 发布
本文介绍了一个Java程序,用于读取员工工时数据,计算总工时,并按降序排序显示每个员工的工时。
2633

被折叠的 条评论
为什么被折叠?



