打印正方形整数矩阵

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值