Java 如何让用户按降序输出?
我尝试过使用Java 如何让用户按降序输出?,java,arrays,eclipse,Java,Arrays,Eclipse,我尝试过使用Arrays.sort(int,Collections.reverseOrder()) 但这似乎不起作用。我试图按降序得到输出。它必须在for循环中吗?或者在我最后使用的System.out.print()中 public class EmployeeWorkHours { public static void main(String[] args) { Scanner turtle = new Scanner(System.in);
Arrays.sort(int,Collections.reverseOrder())代码>
但这似乎不起作用。我试图按降序得到输出。它必须在for循环中吗?或者在我最后使用的System.out.print()
中
public class EmployeeWorkHours {
public static void main(String[] args) {
Scanner turtle = new Scanner(System.in);
String[] calender = { "S", "M", "T", "W", "Th", "F", "S" };
System.out.println("How many Employee's do you have?: ");
int NUMBER_OF_EMPLOYEES = turtle.nextInt();
turtle.nextLine();
int [][]hours;
int[] totalHours= new int[NUMBER_OF_EMPLOYEES];
hours = new int[NUMBER_OF_EMPLOYEES][7];
String[] employee = new String[NUMBER_OF_EMPLOYEES];
// input for Names
for (int x = 0; x < (employee.length); x++) {
System.out.println("Name of Employee " + (x + 1) + ": ");
String name = turtle.nextLine();
employee[x] = name;
}
// input for Hours
for (int z = 0; z < employee.length; z++) {
System.out.println("Starting from Sunday, enter the hours Employee "+ (z + 1)+ " has worked each day (Make sure you seperate it by spaces): ");
for (int a = 0; a < 7; a++) {
hours[z][a] = turtle.nextInt();
}
turtle.nextLine();
}
// Print everything out
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];
}
System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");
}
}
}
公共类员工工作时间{
公共静态void main(字符串[]args){
扫描器海龟=新扫描器(System.in);
字符串[]日历={“S”、“M”、“T”、“W”、“Th”、“F”、“S”};
System.out.println(“您有多少员工?:”;
int NUMBER_OF_EMPLOYEES=turtle.nextInt();
海龟。下一行();
整数[][]小时;
int[]总小时数=新int[员工人数];
小时数=新整数[员工人数][7];
String[]employee=新字符串[员工人数];
//输入名称
对于(int x=0;x<(employee.length);x++){
System.out.println(“员工姓名”+(x+1)+“:”;
字符串名称=turtle.nextLine();
员工[x]=姓名;
}
//输入小时数
对于(intz=0;z
或
这还取决于您正在使用的集合的实现。试着看一下
更具体地说,这是链接的一部分
Comparator cmp = Collections.reverseOrder();
// sort the list
Collections.sort(list, cmp);
System.out.println("List sorted in ReverseOrder: ");
for(int i : list){
System.out.println(i+ " ");
}
你能展示一下你写的代码吗?好的,在上面。。。
Collections.sort(list);
Collections.reverse(list);
Comparator cmp = Collections.reverseOrder();
// sort the list
Collections.sort(list, cmp);
System.out.println("List sorted in ReverseOrder: ");
for(int i : list){
System.out.println(i+ " ");
}