gpt4 book ai didi

java - java程序每周工作时间

转载 作者:行者123 更新时间:2023-12-02 03:42:34 25 4
gpt4 key购买 nike

这里我的输出是:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com