每周工作时间java程序
这里我的输出是:每周工作时间java程序,java,arrays,sorting,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 E
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");
}
}
/**主方法**/
公共静态void main(字符串[]args)
{
/**雇员每周工作时间**/
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[]总计=计算总计(小时);
显示(选择排序(总计));
}
/**总小时数**/
公共静态int[]calculateTotal(int[][]数组)
{
整数[]总小时数=新整数[8];
对于(int i=0;i<8;i++)
{
整数和=0;
对于(int j=0;j<7;j++)
{
总和+=数组[i][j];
总小时数[i]=总和;
}
}
返回总小时数;
}
/**选择排序**/
公共静态int[]选择排序(int[]列表)
{
对于(int i=0;i
我对你的代码做了一些修改。问题是您丢失了员工的初始索引
/**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}};
Employee[] total = employees(hours);
display(selectionSort(total));
}
static class Employee {
private String name;
private int total;
public Employee(String name, int total) {
this.name = name;
this.total = total;
}
public int getTotal() {
return total;
}
public String getName() {
return name;
}
}
/**Total Hours**/
public static Employee[] employees(int[][] array)
{
Employee [] totalHours = new Employee[8];
for (int i = 0; i < 8; i++)
{
int sum = 0;
for (int j = 0; j < 7; j++)
{
sum += array[i][j];
}
totalHours[i] = new Employee("Employee" + i, sum);
}
return totalHours;
}
/**Selection Sort**/
public static Employee[] selectionSort(Employee[] list)
{
for (int i = 0; i < list.length-1; i++)
{
int currentMax = list[i].getTotal();
int currentMaxIndex = i;
for (int j = i + 1; j < list.length; j++)
{
if (currentMax < list[j].getTotal())
{
currentMax = list[j].getTotal();
currentMaxIndex = j;
}
}
if (currentMaxIndex != i)
{
final Employee employee = list[currentMaxIndex];
list[currentMaxIndex] = list[i];
list[i] = employee;
}
}
return list;
}
/**Display**/
public static void display(Employee[] list)
{
for (int i = 0; i < list.length; i++) {
final Employee employee = list[i];
System.out.print(employee.getName() + ": " + employee.getTotal() + " hours \n");
}
}
/**主方法**/
公共静态void main(字符串[]args)
{
/**雇员每周工作时间**/
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}};
员工[]总数=员工(小时);
显示(选择排序(总计));
}
静态类员工{
私有字符串名称;
私人整数合计;
公共雇员(字符串名称,整数总计){
this.name=名称;
这个.总计=总计;
}
公共int getTotal(){
返回总数;
}
公共字符串getName(){
返回名称;
}
}
/**总小时数**/
公共静态雇员[]雇员(int[][]数组)
{
员工[]总工时=新员工[8];
对于(int i=0;i<8;i++)
{
整数和=0;
对于(int j=0;j<7;j++)
{
总和+=数组[i][j];
}
总时数[i]=新员工(“员工”+i,总和);
}
返回总小时数;
}
/**选择排序**/
公共静态员工[]选择排序(员工[]列表)
{
对于(int i=0;i
我对你的代码做了一些修改。问题是您丢失了员工的初始索引
/**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}};
Employee[] total = employees(hours);
display(selectionSort(total));
}
static class Employee {
private String name;
private int total;
public Employee(String name, int total) {
this.name = name;
this.total = total;
}
public int getTotal() {
return total;
}
public String getName() {
return name;
}
}
/**Total Hours**/
public static Employee[] employees(int[][] array)
{
Employee [] totalHours = new Employee[8];
for (int i = 0; i < 8; i++)
{
int sum = 0;
for (int j = 0; j < 7; j++)
{
sum += array[i][j];
}
totalHours[i] = new Employee("Employee" + i, sum);
}
return totalHours;
}
/**Selection Sort**/
public static Employee[] selectionSort(Employee[] list)
{
for (int i = 0; i < list.length-1; i++)
{
int currentMax = list[i].getTotal();
int currentMaxIndex = i;
for (int j = i + 1; j < list.length; j++)
{
if (currentMax < list[j].getTotal())
{
currentMax = list[j].getTotal();
currentMaxIndex = j;
}
}
if (currentMaxIndex != i)
{
final Employee employee = list[currentMaxIndex];
list[currentMaxIndex] = list[i];
list[i] = employee;
}
}
return list;
}
/**Display**/
public static void display(Employee[] list)
{
for (int i = 0; i < list.length; i++) {
final Employee employee = list[i];
System.out.print(employee.getName() + ": " + employee.getTotal() + " hours \n");
}
}
/**主方法**/
公共静态void main(字符串[]args)
{
/**雇员每周工作时间**/
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}};
员工[]总数=员工(小时);
显示(选择排序(总计));
}
静态类员工{
私有字符串名称;
私人整数合计;
公共雇员(字符串名称,整数总计){
this.name=名称;
这个.总计=总计;
}
公共int getTotal(){
返回总数;
}
公共字符串getName(){
返回名称;
}
}
/**总小时数**/
公共静态雇员[]雇员(int[][]数组)
{
员工[]总工时=新员工[8];
对于(int i=0;i<8;i++)
{
整数和=0;
对于(int j=0;j<7;j++)
{
总和+=数组[i][j];
}
总时数[i]=新员工(“员工”+i,总和);
}
返回总小时数;
}
/**选择排序**/
公共静态员工[]选择排序(员工[]列表)
{
对于(int i=0;i /**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);
}
/**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, int[] originList)
{
for (int i = 0; i < list.length; i++)
{
int index = i;
for (int j = 0; j < list.length; j++)
{
if (list[i] == originList[j])
index = j;
}
System.out.print("Employee" + index + ": " + list[i] + " hours \n");
}
}
TreeSet<Employee> employees = new TreeSet<Employee>();
employees.add(new Employee(0,34));
employees.add(new Employee(1,28));
employees.add(new Employee(2,20));
employees.add(new Employee(3,31));
employees.add(new Employee(4,32));
employees.add(new Employee(5,28));
employees.add(new Employee(6,37));
employees.add(new Employee(7,41));
for (Employee employee : employees.descendingSet()) {
System.out.print("Employee" + employee.getId() + ": " + employee.getHours() + " hours \n");
}
public class Employee implements Comparable<Employee>
{
private Integer id;
private Integer hours;
public Employee(int id, int hours)
{
this.id = id;
this.hours = hours;
}
public int getId()
{
return id;
}
public int getHours()
{
return hours;
}
@Override
public int compareTo(Employee o) {
int hoursComparison = hours.compareTo(o.getHours());
return hoursComparison == 0 ? id.compareTo(o.getId()) : hoursComparison;
}
}