(1)键盘录入6个int类型的数据存数数组arr中
(2)将arr数组中的内容反转
(3)将翻转后的数组角标为奇数的互相交换 1和3换, 3和5换,以此类推
(4)最后将数组最后一个角标为奇数的元素 和数组中第一个角标为奇数的元素交换
(5)打印最终的数组(实现了1-4步之后的数组)
(6)如:用户输入的6个int数字为[1,2,3,4,5,6],最后输出的结果为[6, 5, 4, 1, 2, 3]
*/
import java.util.Scanner;
class Aarray_Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数:");
int x0 = sc.nextInt();
System.out.println("请输入整数:");
int x1 = sc.nextInt();
System.out.println("请输入整数:");
int x2 = sc.nextInt();
System.out.println("请输入整数:");
int x3 = sc.nextInt();
System.out.println("请输入整数:");
int x4 = sc.nextInt();
System.out.println("请输入整数:");
int x5 = sc.nextInt();
int[] arr = {x0,x1,x2,x3,x4,x5};
//int[] arr = {1,2,3,4,5,6};
reverse(arr);
huhuan(arr);
//print(arr);
huanhuan(arr);
}
public static void reverse(int[] arr) {
int temp = 0;
for (int i =0;i < arr.length/2 ;i++ ){
temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
}
public static void huhuan(int[] arr) {
int temo = 0;
for (int j =1;j < arr.length && j+2<=5 ;j+=2 ){
temo = arr[j];
arr[j] = arr[j + 2];
arr[j + 2] = temo;
}
}
/*public static void print(int[] arr) {
for (int k =0;k < arr.length ;k++ ){
System.out.print(arr[k]+" ");
} //6 3 4 1 2 5
}*/
public static void huanhuan(int[] arr) {
int erro = 0;
for (int h =0;h < arr.length ;h++ ){
if (h == 1){
erro = arr[1];
arr[1] = arr[5];
arr[5] = erro;
}
System.out.print(arr[h]+" ");
}
}
}
|
|