import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建一个Scanner对象
Scanner sc = new Scanner(System.in);
// 创建一个长度为10的数组
int[] a = new int[10];
int c;
// 输出提示信息
System.out.printf("请输入十个数:");
// 遍历数组,输入数据
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
// 定义两个指针,分别指向数组的头尾
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
// 交换头尾指针指向的元素
c = a[i];
a[i] = a[j];
a[j] = c;
}
// 输出交换后的数组
System.out.printf("交换后的数组为:");
for (int i = 0; i < a.length; i++) {
System.out.printf("%-4d ", a[i]);
}
}
}
JAVA10个元素数组反转(代码中含有注释)
于 2023-12-14 19:59:07 首次发布