每周工作时间java程序

时间:2016-04-17 00:23:18

标签: 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");

    }

}

4 个答案:

答案 0 :(得分:1)

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

您可以考虑在calculateTotal中返回int [] []。它的形式为[[employeeNumber,numberOfHours]]。您示例中calculateTotal的结果看起来像

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

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

答案 1 :(得分:1)

所以,首先它不会起作用,因为你在排序数组时丢失了原始索引。

最好的OO方法是使用一个存储小时数和员工身份的类。

以下是TreeSet的解决方案:

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;
  }
}

答案 2 :(得分:0)

我对你的代码做了一些修改。问题是你丢失了员工的初始索引。

    /**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");
        }
    }

答案 3 :(得分:0)

那是因为当您对总小时数进行排序时,您丢失了每个总小时数的索引(员工编号)。

使用课程是最简单的方法。但是考虑到你可能只想使用静态方法,这里是一种非类,但不是很有效的方法。

    /**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");

   }

}