写了一个小时,居然没分啊!!!算了给你了
import java.util.Scanner;
public class Helix {
/**
* 螺旋输出
*/
public static void main(String[] args) {
int size=5;
Scanner sc=new Scanner(System.in);
System.out.println("输入数组大小:");
size=sc.nextInt();
int count=0;
int [][]array=new int[size][size];
int m=0,n=0;
int down,right,up=0,left=0;
down=size-1;
right=size-1;
int max=size*size;
while(true){
count++;
if(m==up){
array[m][n]=count;
n++;
if(n>right){
n=right;
m++;
}
}
else if(n==right){
array[m][n]=count;
m++;
if(m>down){
m=down;
right--;
}
}
else if(m==down){
array[m][right]=count;
right--;
if(right
down--;
}
}
else if(right==left){
array[down][left]=count;
down--;
if(down<=up){
down=up;
left++;
}
}
else{
up++;
down=n-1;
right=m-1;
m=up;
n=left;
count--;
}
/////////////////////////////////////////////
if(count>=max){
break;
}
}// end loop
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}
第一个太复杂没时间给你考虑
后面一个很简单
规律是这样的
1到n
2到2n
3到3n
。
。
。
n到n*n
两个for循环就出来了