2011-01-17 回答
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
String strIn = "";
System.out.print("请输入矩阵的行列数:");
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);
int n = int1;
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();
}
}
}
追问:
int[][] array = new int[n][n];这是什么数组?
追答:
2维数组呀