- /**
- * @Description 打印螺旋矩形
- *
- * @author Amos.young
- * @date 2015年11月17日
- */
- public class ArrayPrint {
- private int N; // 矩形的维数
- private int[][] array;
- public ArrayPrint() {
- N = 5;
- array = new int[N][N];
- }
- public ArrayPrint(int n) {
- N = n;
- array = new int[N][N];
- }
- public void print() {
- int i = 0, j = 0, i0 = 0, j0 = 0, count = 1, num = N - 1;
- while (num > 0) {
- for (i = i0, j = j0; j < num; j++, count++) {
- array[i][j] = count;
- }
- for (i = i0; i < num; i++, count++) {
- array[i][j] = count;
- }
- for (j = num; j > i0; j--, count++) {
- array[i][j] = count;
- }
- for (i = num; i > i0; i--, count++) {
- array[i][j] = count;
- }
- i0++;
- j0++;
- num--;
- }
- if (N % 2 != 0) {
- array[N / 2][N / 2] = count;
- }
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- String ar = "0";
- if (array[i][j] / 10 < 1)
- ar = "\t" + array[i][j];
- else if (array[i][j] / 100 < 1 && array[i][j] / 10 >= 1)
- ar = "\t" + array[i][j];
- System.out.print(ar + "\t");
- }
- System.out.println();
- System.out.println();
- }
- }
- public static void main(String[] args) throws IOException {
- int order = 0;
- System.out.println("Please input the order of the square:(< 10)");
- order = System.in.read();
- order = order - '0';
- ArrayPrint arrayPrint = new ArrayPrint(order);
- arrayPrint.print();
- }
- }
复制代码 |
|