java从键盘输入5个整数存入数组,由大到小排序后输出数组中所有元素。(必须使用循环)
时间: 2024-05-07 18:15:17 浏览: 84
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.print("请输入第" + (i + 1) + "个整数:");
arr[i] = scanner.nextInt();
}
//排序
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
阅读全文