网上大神是这么写的,自己分析了半天算法逻辑,懵逼了。一起看看吧。
import java.io.*;
public class RingDemo {
public void setArray(int n) {//3
System.out.println("这是" + n + "个螺线型数组:");
int intA = 1;// 初始化
int[][] array = new int[n][n];
int intB;
if (n % 2 != 0) {//3%2==1
intB = n / 2 + 1;// 奇数时i循环次数//2
} else
intB = n / 2;// 偶数时i循环次数
for (int i = 0; i < intB; i++) {// 从外到里循环//2
// 从左到右横的开始
for (int j = i; j < n - i; j++) {
array[i][j] = intA;
intA++;//3
}//1,2,3
// 从上到下纵
for (int k = i + 1; k < n - i; k++) {
array[k][n - i - 1] = intA;
intA++;
}// ,3,
// 从右到左横
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);
}
} |