员工每周工作时间

时间:2014-10-01 18:23:59

标签: java arrays sorting

这就是问题所在:写一个显示员工的程序及其总小时数的减少。我不知道如何编写排序编码。我只是一个初学者:/

如何将它们组合成一个程序并按递减顺序获取输出?

            Su  M   T   W   H   F   Sa
Employee 0  2   4   3   4   5   8   8
Employee 1  7   3   4   3   3   4   4
Employee 2  3   3   4   3   3   2   2
Employee 3  9   3   4   7   3   4   1
Employee 4  3   5   4   3   6   3   8
Employee 5  3   4   4   6   3   4   4
Employee 6  3   7   4   8   3   8   4
Employee 7  6   3   5   9   2   7   9

这是我对显示器的编码:

import java.util.Scanner;

public class WeeklyHours {
    private static int[][] hours;
    public static void main(String[] args) {

          int[][] WeeklyHours = new int[8][7];

          WeeklyHours [0][0] = 2;
          WeeklyHours [0][1] = 4;
          WeeklyHours [0][2] = 3;
          WeeklyHours [0][3] = 4;
          WeeklyHours [0][4] = 5;
          WeeklyHours [0][5] = 8;
          WeeklyHours [0][6] = 8;

          WeeklyHours [1][0] = 7;
          WeeklyHours [1][1] = 3;
          WeeklyHours [1][2] = 4;
          WeeklyHours [1][3] = 3;
          WeeklyHours [1][4] = 3;
          WeeklyHours [1][5] = 4;
          WeeklyHours [1][6] = 4;

          WeeklyHours [2][0] = 3;
          WeeklyHours [2][1] = 3;
          WeeklyHours [2][2] = 4;
          WeeklyHours [2][3] = 3;
          WeeklyHours [2][4] = 3;
          WeeklyHours [2][5] = 2;
          WeeklyHours [2][6] = 2;

          WeeklyHours [3][0] = 9;
          WeeklyHours [3][1] = 3;
          WeeklyHours [3][2] = 4;
          WeeklyHours [3][3] = 7;
          WeeklyHours [3][4] = 3;
          WeeklyHours [3][5] = 4;
          WeeklyHours [3][6] = 1;

          WeeklyHours [4][0] = 3;
          WeeklyHours [4][1] = 5;
          WeeklyHours [4][2] = 4;
          WeeklyHours [4][3] = 3;
          WeeklyHours [4][4] = 6;
          WeeklyHours [4][5] = 3;
          WeeklyHours [4][6] = 8;

          WeeklyHours [5][0] = 3;
          WeeklyHours [5][1] = 4;
          WeeklyHours [5][2] = 4;
          WeeklyHours [5][3] = 6;
          WeeklyHours [5][4] = 3;
          WeeklyHours [5][5] = 4;
          WeeklyHours [5][6] = 4;

          WeeklyHours [6][0] = 3;
          WeeklyHours [6][1] = 7;
          WeeklyHours [6][2] = 4;
          WeeklyHours [6][3] = 8;
          WeeklyHours [6][4] = 3;
          WeeklyHours [6][5] = 8;
          WeeklyHours [6][6] = 4;

          WeeklyHours [7][0] = 6;
          WeeklyHours [7][1] = 3;
          WeeklyHours [7][2] = 5;
          WeeklyHours [7][3] = 9;
          WeeklyHours [7][4] = 2;
          WeeklyHours [7][5] = 7;
          WeeklyHours [7][6] = 9;

          Scanner input = new Scanner(System.in);

          int rows = 8;
          int columns = 7;

          int i,j;


          for (i = 0; i < rows; i++) {

            for(j = 0; j < columns; j++) {
                System.out.print(WeeklyHours[i][j] + " ");
                }
                System.out.println(" ");
          } 

          {



     }

这是我每周总时数的编码:

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

            for (int i = 0; i < hours.length; i++) {
                int sum = totalHours(hours, i);
                System.out.println("Employee " + i + ": " + sum);
            }
        }

        public static int totalHours(int[][] time, int rowIndex) {

            int total = 0;
            int i = 0;
            for (int column = 0; column < time[i].length; column++) {
                total += time[rowIndex][column];
            }
        return total;

4 个答案:

答案 0 :(得分:0)

将总数存储在数组中,然后对此数组进行排序

//we suppose that sumArray is the array to sort
int temp;
for(int i = 0; i < sumArray.length - 1; i++) {
    for(int j = 1; j<sumArray.length; j++) {
        if(sumArray[i] > sumArray[j]) {
            temp = sumArray[i];
            sumArray[i] = sumArray[j];
            sumArray[j] = temp;
        } 
    }
}
//this will sort it in an increasing order 

答案 1 :(得分:0)

public static void main(String... args) {
        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}};
        Map<String, Integer> unsortedData = new HashMap<>();
        int employeeNumber = 0;
        for (int[] employeeHours : hours) {
            int dailyTotal = 0;
            for (int i : employeeHours) {
                dailyTotal += i;
            }
            unsortedData.put("Employee " + employeeNumber++, dailyTotal);
        }
        List<Entry<String, Integer>> sortedData = new ArrayList<>(unsortedData.entrySet());
        Collections.sort(sortedData, new Comparator<Entry<String, Integer>>() {
            @Override
            public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {
                return a.getValue() - b.getValue();
            }
        });
        System.out.println(sortedData);
    }

答案 2 :(得分:0)

首先,删除

    private static int[][] hours;//and
    Scanner input = new Scanner(System.in);
来自WeeklyHours课程的

。然后从int i,j; - 此行开始删除所有内容并使用以下代码

    int[][] hours = new int[8][2];//to store the employee no and no of hours

    for (i = 0; i < rows; i++) {
        hours[i][0] = i;
        for(j = 0; j < columns; j++) {
            hours[i][1] += WeeklyHours[i][j];
        }
    }
    //To print a 2d array use
    System.out.println(Arrays.deepToString(hours));

    Arrays.sort(hours, new ColumnComparator(1));
    System.out.println(Arrays.deepToString(hours));
}//end of main method

static class ColumnComparator implements Comparator<Object> {

    int columnToSort;
    ColumnComparator(int columnToSort) {
        this.columnToSort = columnToSort;
    }
    @Override
    public int compare(Object o1, Object o2) {
        int[] row1 = (int[]) o1;
        int[] row2 = (int[]) o2;
        //compare the columns to sort
        return Integer.valueOf(row1[columnToSort]).compareTo(row2[columnToSort]);
    }
  }
}//end of class

来自this链接的ColumnComparator。

答案 3 :(得分:0)

package Chapter8;

import java.util.Scanner;

public class Exercise8_4 {

  public static void main(String[] args) {
      // Compute the weekly hours for each employee

      Scanner input = new Scanner(System.in);
      int numEmployees;
      final int LABORDAYS = 7;

      System.out.println("Enter number of employees");
      numEmployees = input.nextInt();

      double[][] hours = new double[numEmployees][LABORDAYS];
      double[][] sum = new double[numEmployees][2];

      if (numEmployees > 0) {
          enterHours(hours, LABORDAYS,sum);
          displayEmployeesDecreasingHours(sum);
      } else {
        System.out.println("Wrong entrance");
      }
  }

  public static void enterHours(double[][] hours, int LABORDAYS, double[][] sum) {
      Scanner input = new Scanner(System.in);

      for (int i = 0; i < hours.length; i++) {
          System.out.println("Enter hours for the " + LABORDAYS + " days for Employee " + i);
          for (int j = 0; j < LABORDAYS; j++) {
              hours[i][j] = input.nextDouble(); 
              sum[i][0] = sum[i][0] + hours[i][j];
              sum[i][1] = i;
          }
      }
  }

  public static void displayEmployeesDecreasingHours(double[][] sum) {

      for (int i = 0; i < sum.length; i++) {
          for (int j = i+1; j < sum.length;j++) {
              if (sum[i][0] < sum[j][0]) {
                  double temp = sum[i][0];
                  double tempEmp = sum[i][1];
                  sum[i][0] = sum[j][0];
                  sum[i][1] = sum[j][1];
                  sum[j][0] = temp;
                  sum[j][1] = tempEmp;
              }
          }
      }

      for (int i = 0; i < sum.length; i++) {
           System.out.println("Employee " + (int)sum[i][1] + " worked " + sum[i][0] + " hours;");
       }
   }
}