话不多说,直接上代码
`
/**
* 给数组填数
*
* @param num
* @return
*/
public static int[][] generateHui(int num){
//定义二维数组
int[][] hui = new int[num][num];
int ci = num;
//将二维数组循环填数,按顺时针填,每一圈是一个循环
for (int h = 0; h<=ci/2; h++){
for (int j = h; j<num; j++){
//附初始值
if(h == 0 && j == 0){
hui[h][j] = 1;
continue;
}
//第一行
hui[h][j] = hui[h][j-1]+1;
}
//最后一列
for (int i = h+1; i<num; i++){
hui[i][num-1] = hui[i-1][num-1]+1;
}
//最后一行
for (int j= num-1;j>h;j--){
hui[num-1][j-1] = hui[num-1][j]+1;
}
//第一列
for (int i = num-1;i>h; i--){
if(i-1!=h){
hui[i-1][h] = hui[i][h] + 1;
}
}
num --;
}
return hui;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
int[][] hui = generateHui(7);
for (int i = 0;i<hui.length;i++){
for (int j = 0;j<hui[i].length;j++){
System.out.print(hui[i][j]+" ");
}
System.out.println();
}
}`