下面是一个java分配问题,我已经完成了大部分内容,但是我在按降序显示输出时遇到了一个问题(从最高工作小时数开始到最低工作小时数,同时还要将这些小时数与工作他们的员工相匹配)。我用粗体突出显示了问题中需要帮助的部分,并包含了示例输出。
问题:
计算员工的每周工作时间假设所有员工的每周工作时间都存储在二维数组中。每行记录员工的n天工作时间,其中n列≥ 1和n≤ 7代表这些员工每周工作的天数。例如,下表表示一个数组,该数组存储8名员工一周7天的工作时间。写一个程序,把员工人数和一周的工作天数作为输入。然后它接收所有员工信息(姓名和每日工作小时数)。此程序应按总工时的降序显示员工及其一周的总工时。
样本输出:
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");
}
}
}
1条答案
按热度按时间zyfwsgd61#
下面是一个快速但性能不佳的代码修补程序:
更好的方法是把你的
EmployeeWorkHours
由Collections.sort()
就像安德烈亚斯建议的那样。请看这里的示例代码。