class luoXuanFangZhen
{
public void printluoXuanFangZhen()
{
Scanner sc = new Scanner(System.in);
{
System.out.println("请输入方阵的长度");
}
int SIZE = sc.nextInt();;
int arr[][] = new int[SIZE][SIZE];
int i, j, k = 0, n=SIZE;
for (i = 0; i < n; i++)
{
for (j = i; j <= n - i - 1; j++)
arr[i][j] = ++k;
for (j = i + 1; j <= n - i - 1; j++)
arr[j][n - i - 1] = ++k;
for (j = n - i - 2; j >= i; j--)
arr[n - i - 1][j] = ++k;
for (j = n - i - 2; j >= i + 1; j--)
arr[j][i] = ++k;
}
for(int h=0;h<arr.length;h++)
{
for(int t=0;t<arr.length;t++)
{
System.out.print(arr[h][t]+"\t");
}
System.out.println();
System.out.println();
}
}
} |