java string转rect_java 矩形输出

这个还不是很懂用2维数组写的

贴代码

package com.sec.example;

import java.io.IOException;

import java.util.Calendar;

import java.util.Date;

public class Test {

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';

Test arr=new Test(order);

arr.Print();

}

private int N; //矩阵的维数

private int[][] array;

Test(){

N=5;

array=new int[N][N];

}

Test(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

array[i][j]=count;

}

for (i=i0;i

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

for (j=0;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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值