for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
System.out.print(arys[i][j] + "\t");
}
System.out.println();
}
}
private static int[][] printCircle(int number) {
int[][] ary = new int[number][number];
int rightLen;
int downLen;
int leftLen;
int upLen;
int rightRow = 0;
int rightCol = 0;
int leftRow = number - 1;
int leftCol = number - 1;
int downRow = 1;
int downCol = number - 1;
int upRow = number - 2;
int upCol = 0;
int digit = 1;
while(number > 0){
rightLen = number;
downLen = number - 1;
leftLen = number - 1;
upLen = number - 2;
//Step 1: print to right
for(int i = 0, temp = rightCol; i < rightLen; i++){
ary[rightRow][temp++] = digit++;
}
//Step 2: print to down
for(int j = 0, temp = downRow; j < downLen; j++){
ary[temp++][downCol] = digit++;
}
//move left
for(int m = 0, temp = leftCol - 1; m < leftLen; m++){
ary[leftRow][temp--] = digit++;
}
//move up
for(int n = 0, temp = upRow; n < upLen; n++){
ary[temp--][upCol] = digit++;
}
number = number - 2;
//increas/decrease the row and column
rightRow++;
rightCol++;
downRow++;
downCol--;
leftRow--;
leftCol--;
upRow--;
upCol++;
}
return ary;
}
}
跪求高手,详解代码,写上注释,越详细越好。。。。
彻底迷糊。。。{:soso_e149:}作者: 黑马振鹏 时间: 2012-7-11 21:22
public class Test7 {
public static void main(String[] args) {
int number = 4;
int[][] arys = printCircle(number);
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
System.out.print(arys[i][j] + "\t");
}
System.out.println();
}
}
private static int[][] printCircle(int number) {
//定义一个二维数组
int[][] ary = new int[number][number];
//长度
int rightLen;//右边
int downLen;//下边
int leftLen;//左边
int upLen;//上边
int rightRow = 0;
int rightCol = 0;
int leftRow = number - 1;//3
int leftCol = number - 1;//3
int downRow = 1;
int downCol = number - 1;//3
int upRow = number - 2;//2
int upCol = 0;
int digit = 1;
while(number > 0){
rightLen = number;
downLen = number - 1;//3
leftLen = number - 1;//3
upLen = number - 2;//2
//Step 1: print to right
//从右向左开始打印:
//首先打印第一行for(int i=0,temp =0,i<4;i++)
//rightCol右向行号
for(int i = 0, temp = rightCol; i < rightLen; i++){
ary[rightRow][temp++] = digit++;
}
//move left
//for(int m=0,temp=2;m<3;m++)
//最底层,倒数第二列开始向左打印
for(int m = 0, temp = leftCol - 1; m < leftLen; m++){
ary[leftRow][temp--] = digit++;
}
//move up
//for(int n =0,temp = 2;n<2;n++)
// 打印第一列,从下往上开始
for(int n = 0, temp = upRow; n < upLen; n++){
ary[temp--][upCol] = digit++;
}
//答应一圈以后行和列都减少2,正方形缩小两行两列
number = number - 2;
//increas/decrease the row and column
//变量变化
rightRow++;
rightCol++;
downRow++;
downCol--;
leftRow--;
leftCol--;
upRow--;
upCol++;
}
return ary;
}
}作者: 孙浩迪 时间: 2012-7-11 21:44
啸傲江湖007 发表于 2012-7-11 21:22
public class Test7 {
public static void main(String[] args) {
public class Main {
/**
* 回形打印数字
* @param args
*/
public static void main(String[] args){
int num=11; //这个是需要打印的数组的长和宽度,测试时请改变这个
int flag=num;
int number=1;
int[][] shuZu=new int[num][num];