实现如上图所示
不管哪一行哪一列最后实现和都为同一个数从第一行中间开始1
代码如下
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入大于三的奇数");
int x = sc.nextInt();
int h = 0;
int l = x / 2; //确定第一行中间位置
int[][] a = new int[x][x];
for (int i = 1; i <= x * x; i++) {
a[h][l] = i;
h--;
l++;
// 行越界,列也越界
if (h < 0 && l >= x) {
h+=2;
l--;
// 行越界,列没有越界
} else if (h < 0) {
h = x - 1;
// 列越界,行没有越界
} else if (l >= x) {
l = 0;
//如果移动到的坐标数组上面有数字的话