class ArrayTest {
public static void main(String[] args) {
int sum = 9;
int[][] arr = new int[sum][sum];
int x = 0;
int y = 0;
int count = 0;
int flag = 1;
int line = arr.length - 1;
while (y <= line) {
count++;
arr[x][y] = count;
y++;
}
y--;
luoXuan(arr, x, y, count, flag, line);
}
public static void luoXuan(int[][] arr, int x, int y, int count, int flag,int line) {
int time1 = line;
int time2 = line;
while (time1 > 0) {
time1--;
count++;
x += flag;
arr[x][y] = count;
}
while (time2 > 0) {
time2--;
count++;
y -= flag;
arr[x][y] = count;
}
if (count != line * line) {
line--;
flag = -flag;
luoXuan(arr, x, y, count, flag, line);
} else {
for (int a = 0; a < arr.length; a++) {
for (int b = 0; b < arr[a].length; b++)
System.out.print(arr[a][b] + " \t ");
System.out.println();
}
}
}
}
感觉有点麻烦,不知道这智商能不能混黑马:L
|
|