我在java中做一个升序和降序编号,这是我的代码:
System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
int arr[] = new int[num1];
for (int i = 0; i<num1; i++) {
System.out.print("Enter Value #" + (i + 1) + ":");
arr[i] =Integer.parseInt(in.readLine());
}
System.out.print("Numbers in Ascending Order:" );
for(int i = 0; i < arr.length; i++) {
Arrays.sort(arr);
System.out.print( " " +arr[i]);
}
System.out.println(" ");
System.out.print("Numbers in Descending Order: " );
目前,代码生成以下内容:
Enter How Many Inputs: 5
Enter Value #1:3
Enter Value #2:5
Enter Value #3:6
Enter Value #4:11
Enter Value #5:2
Numbers in Ascending Order: 2 3 5 6 11
Numbers in Descending Order:
因此,该Arrays.sort(arr)
调用似乎有效-但我正在寻找一种类似的简单方法来提供降序排序,但在文档中找不到它。有任何想法吗?