java - java程序每周工作时间

标签 java arrays sorting

这里我的输出是:

Employee0: 41 hours 
Employee1: 37 hours 
Employee2: 34 hours 
Employee3: 32 hours 
Employee4: 31 hours 
Employee5: 28 hours 
Employee6: 28 hours 
Employee7: 20 hours 

但是我需要将员工信息存储在数组中并获取以下输出:

Employee7: 41 hours 
Employee6: 37 hours 
Employee0: 34 hours 
Employee4: 32 hours 
Employee3: 31 hours 
Employee5: 28 hours 
Employee1: 28 hours 
Employee2: 20 hours 

这是我目前的代码,具有顶部输出。我似乎无法了解如何将员工信息存储在数组中并打印第二个输出。

  /**Main Method**/
    public static void main(String[] args) 
    {
        /**Employee's weekly hours**/
        int[][] hours = {{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[] total = calculateTotal(hours);
        display(selectionSort(total));
    }

    /**Total Hours**/
    public static int[] calculateTotal(int[][] array) 
    {
        int [] totalHours = new int[8];
        for (int i = 0; i < 8; i++) 
        {  
            int sum = 0;  
            for (int j = 0; j < 7; j++) 
            {  
                sum += array[i][j];  
                totalHours[i] = sum;
            }
        }
        return totalHours;
    }

    /**Selection Sort**/
    public static int[] selectionSort(int[] list) 
    {

        for (int i = 0; i < list.length-1; i++) 
        {

            int currentMax = list[i];
            int currentMaxIndex = i;
            for (int j = i + 1; j < list.length; j++) 
            {
                if (currentMax < list[j]) 
                {
                    currentMax = list[j];
                    currentMaxIndex = j;
                }
            }

            if (currentMaxIndex != i) 
            {
                list[currentMaxIndex] = list[i];
                list[i] = currentMax;
            }
        }
        return list;
    }

    /**Display**/
    public static void display(int[] list)
    {
        for (int i = 0; i < list.length; i++)
            System.out.print("Employee" + i + ": " + list[i] + " hours \n");

    }

}

最佳答案

您永远不会将员工映射到他们的工作时间。

您可以考虑在 calculateTotal 中返回 int[][]。它的形式为 [[employeeNumber, numberOfHours]]。您的示例中 calculateTotal 的结果如下所示

[
 [0,34],
 [1,28],
 [2,20],
 [3,31],
 [4,32],
 [5,28],
 [6,37],
 [7,41]
] 

然后,您的选择排序方法必须按每个嵌套 int[] 的索引 1 进行排序。您还需要重写您的 display 方法以适应数据结构的更改

关于java - java程序每周工作时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671338/

相关文章:

java - ruby 中的对象引用类型

java - 无法在线从wamp服务器访问mysql数据库

php - MySQL JOIN 关联列在数组中具有多个行值

c - 整数被重复?

javascript - 一组 HTML 元素的倒序

Perl - 从引用的@array 中删除重复项

java - spring mvc 中的 Bean 验证和请求参数

java - 层次继承的正确UML是什么?

java - 通过排列的动态循环量

javascript - 如何获取没有 ID 的对象数组之间的差异