for迴圈輸出菱形的形狀【java】

來源:互聯網
上載者:User

標籤:str   img   分析   public   style   結果   換行   main   ges   

使用for迴圈語句輸出以下“菱形”效果:

    *   ***  ***** **************** *******   *****   ***    *

代碼  

 1 /* 2     * 3    *** 4   ***** 5  ******* 6 ********* 7  *******  8   ***** 9    ***10     *11 12 分析:13     - 上層(共5層)14         每層構成:每層每層空格數+每層星星數15             第1層 -- 空格數:4 + 星星數:116             第2層 -- 空格數:3 + 星星數:317             第3層 -- 空格數:2 + 星星數:518             第4層 -- 空格數:1 + 星星數:719             第5層 -- 空格數:0 + 星星數:920             21             分析:22             第1層 -- 空格數:4(= 5-1) + 星星數:1(= 2*1-1)23             第2層 -- 空格數:3(= 5-2) + 星星數:3(= 2*2-1)24             第3層 -- 空格數:2(= 5-3) + 星星數:5(= 2*3-1)25             第4層 -- 空格數:1(= 5-4) + 星星數:7(= 2*4-1)26             第5層 -- 空格數:0(= 5-5) + 星星數:9(= 2*5-1)27 28             推理:(上層共 layer 層)29             第i層 -- (空格數 = layer-i) + (星星數 = 2*i-1)30 31 32     - 下層(下層共 layer-1 層)33             第1層 -- 空格數:1 + 星星數:734             第2層 -- 空格數:2 + 星星數:535             第3層 -- 空格數:3 + 星星數:336             第4層 -- 空格數:4 + 星星數:137             38             分析:效果等效(共4層)39             下層的第1層對應上層的第4層 -- 空格數:1 + 星星數:740             下層的第2層對應上層的第3層 -- 空格數:2 + 星星數:541             下層的第3層對應上層的第2層 -- 空格數:3 + 星星數:342             下層的第4層對應上層的第1層 -- 空格數:4 + 星星數:143 44 45             推理:46             第i層 -- (空格數 = i) + (星星數 = 2*(layer-i)-1)47 */48 49 50 public class StarTree51 {52     public static void main(String[] args){53         int layer = 5;54 55         // 一、上層(顯示 layer 行)56         for(int i=1;i<=layer;i++){57              58             // 1、擷取空格需要顯示數量59             int spaceNum = layer-i; 60             for(int j=1;j<=spaceNum;j++){  // 需要列印空格的數量61                 System.out.print(" ");62             }63             64             // 2、擷取星星需要顯示的數量65             int starNum = 2*i-1;66             for(int j=1;j<=starNum;j++){ // 需要列印星星的數量67                 System.out.print("*");68             }69             // 3、換行70             System.out.println();71         }72         73         // 二、下層(顯示 layer-1 行)74         for(int i=1;i<layer;i++){75 76             // 1、擷取空格需要顯示數量77             int spaceNum = i;78             for(int j=1;j<=spaceNum;j++){79                 System.out.print(" ");80             }81             // 2、擷取星星需要顯示的數量82             int starNum = 2*(layer-i)-1;83             for(int j=1;j<=starNum;j++){84                 System.out.print("*");85             }86             // 3、換行87             System.out.println();88 89         }90     }91 }

  代碼結果顯示:

 

for迴圈輸出菱形的形狀【java】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.