使用Java打印数字组成的魔方阵及字符组成的钻石图形

时间:2021-11-28 23:58:35

打印魔方阵

输入一个自然数N(2≤N≤9),要求输出如下的魔方阵,即边长为N*N,元素取值为1至N*N,1在左上角,呈顺时针方向依次放置各元素。  N=3时:

?
1
2
3
1  2  3 
8  9  4 
7  6  5

【输入形式】 从标准输入读取一个整数N。 
【输出形式】 向标准输出打印结果。输出符合要求的方阵,每个数字占5个字符宽度,向右对齐,在每一行末均输出一个回车符。
【输入样例】  4
【输出样例】     

?
1
2
3
4
1  2  3  4
12  13  14  5
11  16  15  6 
10  9  8  7

实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cn.dfeng;
import java.util.Arrays;
import java.util.Scanner;
public class Maze {
  enum Direction{
    UP, DOWN, RIGHT, LEFT;
  }
  public int[][] buidMaze( int n ){
    int[][] maze = new int[n][n];
    for( int[] a : maze ){
      Arrays.fill(a, 0);
    }
     
    int col = 0;
    int row = 0;
    int counter = 1;
    Direction d = Direction.RIGHT;
     
    while( true ){
      if( maze[row][col] == 0 ){
        maze[row][col] = counter++;
         
        switch (d) {
          case RIGHT: 
            if( col + 1< n && maze[row][col + 1] == 0){
              col ++;
            }else{
              d = Direction.DOWN;
              row ++;
            }
            break;
          case DOWN:
            if( row + 1 < n && maze[row + 1][col] == 0){
              row ++;
            }else{
              d = Direction.LEFT;
              col --;
            }
            break;
          case LEFT:
            if( col - 1 >= 0 && maze[row][col-1] == 0){
              col --;
            }else{
              d = Direction.UP;
              row --;
            }
            break;
          default:
            if( row - 1 >= 0 && maze[row - 1][col] == 0){
              row --;
            }else{
              d = Direction.RIGHT;
              col ++;
            }
            break;
        }
         
      }else{
        break;
      }
    }
    return maze;
  }
   
  public void printMaze( int[][] maze ){
     
    for( int[] row : maze ){
      for( int i : row ){
        System.out.printf("%3d", i);
      }
      System.out.println();
    }
     
  }
  /**
   * @param args
   */
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input the size of the maze:");
    int size = sc.nextInt();
    Maze maze = new Maze();
    int[][] m = maze.buidMaze( size );
    maze.printMaze( m );
  }
}

打印钻石图形

钻石图的效果大概就是这样的:

使用Java打印数字组成的魔方阵及字符组成的钻石图形

下面我们来看代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cn.dfeng;
/**
 * 该类能够用*打印大小的钻石图形
 * @author dfeng
 *
 */
public class Drawer {
   
  /**
   * 打印钻石图形
   * @param n 钻石大小
   */
  public void printDiamond( int n ){
    System.out.println();
    int i = 0;
    boolean flag = true;
    while( i >= 0 ){
      if (i < n) {
        for (int j = 0; j < n - i; j++) {
          System.out.print(" ");
        }
        for (int j = n - i; j <= n + i; j += 2) {
          System.out.print("* ");
        }
        System.out.println();
      }
      if (i == n) {
        flag = false;
        i--;
      }
      if (flag) {
        i++;
      } else {
        i--;
      }
    }
     
  }
}