国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

使用Java打印數字組成的魔方陣及字符組成的鉆石圖形

2019-11-26 14:29:52
字體:
來源:轉載
供稿:網友

打印魔方陣

輸入一個自然數N(2≤N≤9),要求輸出如下的魔方陣,即邊長為N*N,元素取值為1至N*N,1在左上角,呈順時針方向依次放置各元素。  N=3時:

   1  2  3     8  9  4     7  6  5 

【輸入形式】 從標準輸入讀取一個整數N。 
【輸出形式】 向標準輸出打印結果。輸出符合要求的方陣,每個數字占5個字符寬度,向右對齊,在每一行末均輸出一個回車符。
【輸入樣例】  4
【輸出樣例】     

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

實現:

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 );   } } 

打印鉆石圖形

鉆石圖的效果大概就是這樣的:

201631784713111.png (171×170)

下面我們來看代碼

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--;       }     }        } } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 墨竹工卡县| 三原县| 安宁市| 泗水县| 鄂托克旗| 常宁市| 安化县| 开阳县| 革吉县| 海林市| 平原县| 冀州市| 靖江市| 嘉义市| 上饶县| 沾益县| 永城市| 纳雍县| 西平县| 绥中县| 广元市| 安国市| 东至县| 嘉黎县| 崇仁县| 梓潼县| 方城县| 安徽省| 九江市| 手机| 台南市| 开远市| 华容县| 北流市| 蒙城县| 若尔盖县| 天全县| 凌源市| 凤山市| 屯门区| 民和|