import java.util.Arrays;
import javax.swing.JOptionPane;
public class 回型方阵 {
private static final int UP = 1;
private static final int LEFT = 2;
private static final int RIGHT = 3;
private static final int DOWN = 4;
private static final int[] DIRS = {RIGHT, DOWN, LEFT, UP};
public static void main(String[] args) {
int n = Integer.parseInt(JOptionPane.showInputDialog("请输入一个奇数", "5"));
int a[][] = new int[n][n];
int x = 0, y = 0; //当前位置
int count = 0; //当前方向填充了多少个
int number = 1; //当前填充的数字
int dirIndex = 0; //方向下标
int dir = DIRS[dirIndex]; //方向
int total = n * n;
for (int i = 0; i < total; i++) {
a[y][x] = number++; //填充
count++;
if (count>=n) {
//换方向
if (dir==RIGHT || dir == LEFT) n--; //向左或向右走到头,减一
dirIndex = (dirIndex + 1) % DIRS.length;
dir = DIRS[dirIndex];
count = 0;
}
switch (dir) {
case RIGHT:
x++;
break;
case LEFT:
x--;
break;
case UP:
y--;