回型数
请输入回型数的个数:7
1 2 3 4 5 6 7
24 25 26 27 28 29 8
23 40 41 42 43 30 9
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
public class RectangularNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入回型数的个数:");
int number = scanner.nextInt();
int[][] arr = new int[number][number];
int max_x = number - 1;
int min_x = 0;
int max_y = number - 1;
int min_y = 0;
int add = 1;
while(add <= (number * number)){//当输出的值和回型数个数的平方相等,则表示已经输出完毕,退出循环。
for(int i = min_x; i <= min_x; i++){//上行
for(int j = min_y; j <= max_y ; j++){
arr[i][j] = add;
add ++;
}
}
min_x++;//行数最小值加1
for(int i = min_x; i <= max_x; i++){//右列
for(int j = max_y; j <= max_y ; j++){
arr[i][j] = add;
add ++;
}
}
max_y--;//列数最大值减1
for(int i = max_x; i <= max_x; i++){//下行
for(int j = max_y; j >= min_y ; j--){
arr[i][j] = add;
add ++;
}
}
max_x--;//行数最大值减1
for(int i = max_x; i >= min_x; i--){//左列
for(int j = min_y; j <= min_y ; j++){
arr[i][j] = add;
add ++;
}
}
min_y++;//列数最小值加1
}
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
scanner.close();
}
}