gpt4 book ai didi

Java Eclipse 排序然后排序 1 个数组,也许更多?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:21 24 4
gpt4 key购买 nike

以下是一个 Java 作业问题,我已经完成了大部分,但是我在按降序显示输出时遇到了问题(从最高工作小时数开始到最低工作小时数,同时还将这些时间与工作的员工相匹配)。我用粗体 突出显示了我需要帮助的问题部分,并包含了示例输出。

问题:

计算员工的每周工时假设所有员工的每周工时都存储在一个二维数组中。每行记录一个员工的 n 天工作时间,n 列,其中 n ≥ 1 且 n ≤ 7 表示这些员工一周工作的天数。例如,下表表示一个数组,其中存储了八名员工一周 7 天的工作时间。编写一个程序,将员 worker 数和一周的工作天数作为输入。然后它包含所有员工信息(姓名和每日工作时数)。 此程序应按总时数的降序显示员工及其在一周内的总工作时数。

示例输出:

Employee 7 worked 39 hours

Employee 6 worked 37 hours

Employee 0 worked 34 hours

Employee 4 worked 32 hours

Employee 3 worked 31 hours

Employee 1 worked 28 hours

Employee 5 worked 28 hours

Employee 2 worked 24 hours

这是我到目前为止所得到的,我相信我在最后一个 for 循环中遗漏了一些东西,但这就是我发布这个的原因。非常感谢任何帮助:

import java.util.Scanner;
import java.util.Arrays;

public class EmployeeWorkHours {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many Employee's do you have?: ");
int NUM_OF_EMPLOYEES = scan.nextInt();
scan.nextLine();

int [][]hours;
int []totalHours= new int[NUM_OF_EMPLOYEES];

hours = new int[NUM_OF_EMPLOYEES][7];
String[] employee = new String[NUM_OF_EMPLOYEES];

// input Names
for (int x = 0; x < (employee.length); x++)
{
System.out.println("Name of Employee " + (x + 1) + ": ");
String name = scan.nextLine();
employee[x] = name;

}

// input Hours
for (int z = 0; z < employee.length; z++)
{
System.out.println("Beginning on Monday, enter the hours Employee "+ (z + 1)+ " has worked each day (Separate each day by spaces): ");
for (int a = 0; a < 7; a++)
{
hours[z][a] = scan.nextInt();
}
scan.nextLine();
}

// Print everything in for loop
for (int i = 0; i < employee.length; i++)
{
totalHours[i]=0;
for(int j=0; j<7; j ++)
{
totalHours[i] = totalHours[i]+hours[i][j];
}
// I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
// Arrays.sort(totalHours); gives me the wrong output, I'm stuck

System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");
}
}
}

最佳答案

对您的代码进行快速但性能不佳的补丁:

// Print everything in for loop
for (int i = 0; i < employee.length; i++) {
totalHours[i] = 0;
for (int j = 0; j < 7; j++) {
totalHours[i] = totalHours[i] + hours[i][j];
}
// I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
// Arrays.sort(totalHours); gives me the wrong output, I'm stuck

}

//patch starts here
int[] totalHoursSortedAsc = Arrays.copyOf(totalHours, totalHours.length);
Arrays.sort(totalHoursSortedAsc);
for (int i = totalHoursSortedAsc.length - 1;i>=0;i--) {
for (int j = 0;j < totalHours.length;j++) {
if (totalHoursSortedAsc[i] == totalHours[j]) {
System.out.println("Employee " + (j + 1) + " worked " + totalHours[j] + " hours");
totalHours[j] = -1; //Employees may work the same time
}
}
}

更好的方法是按照 Andreas 的建议,通过 Collections.sort() 对您的 EmployeeWorkHours 进行排序。看here示例代码。

关于Java Eclipse 排序然后排序 1 个数组,也许更多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36253343/

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