java 打印bean JAVA 打印炫酷内容_i++

打印上图的方阵用java

 

1 import java.util.Scanner;
 2 
 3 public class p1143 {
 4 
 5     public static void main(String[] args) {
 6         int i, j, n, k = 1;
 7         int a[][] = new int[100][100];
 8         Scanner reader = new Scanner(System.in);
 9         n = reader.nextInt();
10         for (i = 1; i <= n / 2; i++) {
11             for (j = i; j <= n - i; j++) {
12                 a[i][j] = k;
13                 k++;
14                 //s
15             }
16             for (j = i; j <=n-i; j++) {
17                 a[j][n+1-i] = k;
18                 k++;
19                 //y
20             }
21             for (j = n+1-i ; j >= i+1; j--) {
22                 a[n+1-i][j] = k;
23                 k++;
24                 //x
25             }
26             for (j = n+1-i; j >= i+1; j--) {
27                 
28                 a[j][i] = k;
29                 k++;
30                 //z
31             }
32         }
33         if (n % 2 == 1) {
34             i = (n + 1) / 2;
35             a[i][i] = n * n;
36         }
37         for (i = 1; i <= n; i++) {
38             System.out.println();
39             for (j = 1; j <= n; j++) {
40                 System.out.print(" " + a[i][j]);
41             }
42         }
43     }
44 }