public class huiXingShu {
public static void main(String[] args) {
int vol = 10;
int[][] arr = new int[vol][vol];
int x=0,y=0;
int num = 1;
while(arr[x][y]!=(vol*vol)){
arr[x][y] = num;
if(y != vol-1 && arr[x][y+1] == 0){//向右
if(x>0 && arr[x-1][y] == 0){
x--;//优先向上走
}else{
y++;//然后向右走
}
}else if(x != vol-1 && arr[x+1][y] == 0) {//向下
x++;
}else if(y !=0 && arr[x][y-1] == 0){//向左
y--;
}else if(arr[x-1][y] == 0){//向上
x--;
}
num++;
}
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();
}
}
}
![image-20210905190027205](https://gitee.com/cxg_code/image/raw/master/img/20210905190027.png)