java - Sorting user input and printing out in same category -
my homework work 2-dimensional array receive console input of number of hours each employee (8 total) has worked in 7 day week. i'm total input , print out totals of each employee in descending order.
i've been able figure out how print out user inputs in descending order, only, don't know how print them out corresponding employee #.
import java.util.scanner; import java.util.arrays; public class computingweeklyhours { public static void main(string[] args){ scanner in = new scanner(system.in); int[][] register = new int[8][7]; system.out.println("enter hours each employee"); (int = 0; < register.length; i++) (int j = 0; j < register[i].length; j++){ system.out.printf("employee %d:", i+1); register[i][j] = in.nextint(); } int[] totaledhours = sum(register); arrays.sort(totaledhours); (int k = totaledhours.length-1; k >= 0; k--){ system.out.print(totaledhours[k] + " "); } } public static int[] sum(int[][] total){ int[] totaled = new int[8]; (int = 0; < total.length; i++) (int j = 0; j < total[i].length; j++) totaled[i] += total[i][j]; return totaled; } }
this prints out in descending order.
so how can associate each index employee #?
if employee# mean order in employee details entered, use inner class store employee details employee number , total hours shown below.
class employee{ int employeeno; int totalhours; }
and modify sum() method store array of employee objects instead of array of int(which can store 1 int). here how change sum method below
public static employee[] sum(int[][] total){ employee[] totaled = new employee[8]; (int = 0; < total.length; i++) employee[i].employeeno = i+1; (int j = 0; j < total[i].length; j++) employee[i].totalhours += total[i][j]; return totaled; }
Comments
Post a Comment