我这个简单,容易看,大家给个建议
import *;
public class RingDemo{
public void setArray(int n){
System.out.println("这是"+n+"个螺线型数组:");
int intA=1;//初始化
int[][] array=new int[n][n];
int intB;
if(n%2!=0){
intB = n/2+1;//奇数时i循环次数
}
else
intB = n/2;//偶数时i循环次数
for(int i = 0;i < intB; i++){//从外到里循环
//从左到右横的开始
for(int j=i;j < n-i; j++){
array[i][j] = intA;
intA++;
}
//从上到下纵
for(int k=i+1;k < n-i; k++){
array[k][n-i-1] = intA;
intA++;
}
//从右到左横
for(int l=n-i-2;l >= i ;l--){
array[n-i-1][l] = intA;
intA++;
}
//从下到上纵
for(int m=n-i-2; m > i ; m--){
array[m][i]=intA;
intA++;
}
}
//输出数组
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args){
String strIn = "";
System.out.print("please enter a number:");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader buff=new BufferedReader(input);
try{
strIn=buff.readLine();
}
catch(IOException e){
System.out.println(e.toString());
}
int int1 = Integer.parseInt(strIn);
RingDemo ring = new RingDemo();
ring.setArray(int1);
}
}