import java.io.*;
public class ArrayPrint {
private int N;//矩阵的维数
private int[][] array;
ArrayPrint(){
N=5;
array=new int[N][N];
}
ArrayPrint(int n){
N=n;
array=new int[N][N];
}
public void Print(){
int i=0,j=0,i0=0,j0=0,count=1,Num=N-1;
while(Num>0){
for(i=i0,j=j0;j<Num;j++,count++){
array[i][j]=count;
}
for(i=i0;i<Num;i++,count++){
array[i][j]=count;
}
for(j=Num;j>j0;j--,count++){
array[i][j]=count;
}
for(i=Num;i>i0;i--,count++){
array[i][j]=count;
}
i0++;
j0++;
Num--;
}
if(N%2!=0){
array[N%2][N%2]=count;
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
String ar="0";
if(array[i][j]/10<1)
ar=" "+array[i][j];
else if (array[i][j]/100<1 && array[i][j]/10>=1)
{ar=""+array[i][j];
}
System.out.print(ar+" ");
}
System.out.println();
}
}
public static void main(String args[])throws IOException{
int order=0;
System.out.print("Please input the order of the square:(<10)");
order=System.in.read();
order=order-'0';
ArrayPrint arr=new ArrayPrint(order);
arr.Print();
}
}
结果如下:
Please input the order of the square:(<10)9
1 2 3 4 5 6 7 8 9
32 81 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 0 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17