回形字:从键盘输入一个整数,得到回形字
package com.atguigu.java;
import java.util.Scanner;
public class MyTest {
public static void main(String[] args) {// Math.random() [0,1) [0,30) [1,31)
Scanner scanner = new Scanner(System.in);
int arrLine = scanner.nextInt();
System.out.println(arrLine + "***********");
int[][] arr = new int[arrLine][arrLine];
int mouth;
if(arrLine == 1){
mouth = 1;
}else if(arrLine % 2 == 0){
mouth = arrLine / 2;
}else{
mouth = (arrLine + 1) / 2;
}
int x = 0;
for(int L = 0;L <= mouth - 1;L++){//L = 3;
//上回
for(int i = 0 + L;i < 1 + L;i++){
for(int j = 0 + L;j < arr.length - L;j++){
arr[i][j] = ++x;
}
}
//右回
for(int i = arr.length - 1 - L;i < arr.length - L;i++){
for(int j = 1 + L;j < arr.length - L;j++){
arr[j][i] = ++x;
}
}
//下回
for(int i = arr.length - 1 - L;i < arr.length - L;i++){
for(int j = arr.length - 2 - L;j > -1 + L;j--){
arr[i][j] = ++x;
}
}
//左回
for(int i = arr.length - 2 - L;i > 0 + L;i--){
for(int j = 0 + L;j < 1 + L;j++){
arr[i][j] = ++x;
}
}
}
//遍历
for(int i = 0;i < arr.length;i++){
for(int j = 0;j < arr.length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}