0
import java.util.*;  
import java.util.Arrays;
public class EmployeeWeeklyHours{    

      public static void main(String[] args) {    

               int[][] employeeHours= new int[][] {      
                   { 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 [] finalHours = new int[8];  

               for (int i = 0; i < finalHours.length; i++) {   
                   int total = 0;  
                   for (int j = 0; j < finalHours.length - 1; j++) {  
                       total += employeeHours[i][j];  
                       finalHours[i] = total;  
                   }
               }

               java.util.Arrays.sort(finalHours);

               int[] sort = new int[finalHours.length];

               for (int i = 0; i < finalHours.length; i++) 
                   sort[i] = finalHours[i];        
               for (int i = 7; i > -1; i--) 
                   System.out.println(sort[i]);
      }
}

员工 0:2 4 3 4 5 8 8
员工 1:7 3 4 3 3 4 4
员工 2:3 3 4 3 3 2 2
员工 3:9 3 4 7 3 4 1
员工 4:3 5 4 3 6 3 8
员工 5:3 4 4 6 3 4 4
员工 6:3 7 4 8 3 8 4
员工 7:6 3 5 9 2 7 9

从我的代码中可以看出,我应该按降序列出每个员工的总工作时间。但是,我似乎很难在总工时旁边显示员工编号。

EG “员工 7 工作了 42 小时。

有什么方法可以在排序后的数字旁边列出员工编号,而无需对其进行硬编码?我觉得我的问题有一个简单的答案,但现在什么都没有想到。

4

3 回答 3

0

一种解决方案是使用 TreeMap,因为它会自动对键进行排序。

TreeMap<Integer, Integer> treeMap = 
              new TreeMap<Integer, Integer>(Collections.reverseOrder());
for (int i = 0; i < finalHours.length; i++) {
    treeMap.put(finalHours[i], i);
}

for (Entry<Integer, Integer> entry : treeMap.entrySet()) {
    System.out.println( "Employee " + entry.getValue() + " worked " + entry.getKey() + " hours.");
}
于 2016-01-27T00:47:48.443 回答
-1
for (int i=0; i < finalHours.length; i++) {
    System.out.println(String.format("Employee %d worked %d hours.", i, finalHours[i]));
}

既然你提到了硬编码,当你写一个循环时,而不是在里面放一个神奇的“7”,使用像finalHours.lengthor之类的变量employeeHours.length。同样的原则通常适用:即使您“知道”值将是什么,也要引用变量。

于 2016-01-27T00:37:59.803 回答
-2

如果你想要这个代码?

import java.util.*;  
import java.util.Arrays;
public class EmployeeWeeklyHours{    

      public static void main(String[] args) {    

               int[][] employeeHours= new int[][]{      
               { 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}};  

               String [] finalHours = new String[8];  

               for (int i = 0; i < finalHours.length; i++) 
               {   
                   int total = 0;  
                   for (int j = 0; j < finalHours.length-1; j++) 
                   {  

                       total += employeeHours[i][j];  
                       finalHours[i] = "Employee "+ i+" worked "+total + "hours";
                   }

               }

               java.util.Arrays.sort(finalHours);

               String[] sort = new String[finalHours.length];
               for (int i = 0; i < finalHours.length; i++) 
               {  
                   sort[i] = finalHours[i];  
               }

               for (int i = 7; i > -1; i--) 
               { 
                   System.out.println(sort[i]);
               }

      }
}

结果是

Employee 7 worked 41hours
Employee 6 worked 37hours
Employee 5 worked 28hours
Employee 4 worked 32hours
Employee 3 worked 31hours
Employee 2 worked 20hours
Employee 1 worked 28hours
Employee 0 worked 34hours

使用 int[] 替代 String[] 这样您就可以获得该结果。再见。

于 2016-01-27T00:38:15.543 回答