import java.util.Scanner;
class Day_5
{
public static void main(String[] args)
{
Scanner num = new Scanner(System.in);
System.out.println("请输入想要的螺旋矩阵大小:");
int num1 = num.nextInt();
luoXuan(num1);
}
public static void luoXuan(int length)
{
int x = length;
int y = length;
int[][] arr= new int[length][length];
int n=1;
for (int i=0;i<length; i++)
{
for ( x=i;x<length-i;x++)//上 行 →
{
arr[x][i]=n;
n++;
}
System.out.println(n);
for (y=i+1; y<length-i;y++)//右 列
{
arr[length-1-i][y]=n;
n++;
}
System.out.println(n);
for ( x=length-2-i;x>=i;x--)//下 行
{
arr[x][length-1-i]=n;
n++;
}
System.out.println(n);
for ( y=length-2-i;y>i;y--)//左 列
{
arr[i][y]=n;
n++;
}
System.out.println(n);
}
for (y = 0; y <length; y++)
{
for (x = 0; x <length; x++)
{
System.out.print(arr[x][y] + "\t");
}
System.out.println();
}
}
} |
|