亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java 簡單的圣誕樹

Java 簡單的圣誕樹

郎朗坤 2022-01-19 17:10:24
我是 java 新手,我必須創建一個簡單的 java 程序,以這種形式創建一棵圣誕樹:10|       *       |15=7+1+7 9|      ***      |15=6+3+6 8|     *****     |15=5+5+5 7|    *******    |15=4+7+4 6|   *********   |15=3+9+3 5|  ***********  |15=2+11+2 4| ************* |15=1+13+1 3|***************|15=0+15+0 2|      ***      |15=6+3+6 1|      ***      |15=6+3+6高度(所有自然正數)和材質(在這種情況下,“*”由用戶輸入給出)。這是我已經擁有的,但我不知道如何獲得每行末尾的“|15=7+1+7”和樹底部的樹干。這是我的實際代碼,以及它創建的內容:public class Christmas{    public static void main(String[] args) {        int height = Integer.parseInt(args[0]);        String letters = (args[1]);        char firstLetter = letters.charAt(0);        //System.out.println(height+"   "+firstLetter);             for (int i = 0; i < height; i++) {         // System.out.print((args.length)+"");         int row_number = height-i;         System.out.printf("%2d",row_number);         System.out.print("|");            for (int j = 1; j < height - i; j++){            System.out.print(" ");              }                   for (int k = 0; k < (2 * i + 1); k++){                System.out.print(firstLetter+"");                }         System.out.println();        }    }}輸出:C:\Users\name\Desktop\JavaFolder>javac Christmas.java && java Christmas 10 *OMEGALUL10|         * 9|        *** 8|       ***** 7|      ******* 6|     ********* 5|    *********** 4|   ************* 3|  *************** 2| ***************** 1|*******************如何添加樹干,它總是 3 個字母長,每棵樹的 1/4 大。(四舍五入)和每行末尾的 |15=7+1+7,其中包含: 樹的寬度為左側空格的總和 + 相應行中樹的寬度 + 右側的空格(左對齊)。
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

這是一種不同的方法,它計算一行所需的字符數spaces和數量,然后用于打印行。fillprintf


public static void printChristmasTree(int height, char ch) {

    if (height <= 4)

        throw new IllegalArgumentException("Height must be 5 or higher");

    for (int row = height; row > 0; row--) {

        int spaces = (row > 2 ? row - 3 : height - 4);

        int fill = (height - spaces) * 2 - 5;

        System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,

                          repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),

                          spaces * 2 + fill, spaces, fill, spaces);

    }

}

private static String repeat(int count, char ch) {

    char[] buf = new char[count];

    java.util.Arrays.fill(buf, ch);

    return new String(buf);

}

測試


printChristmasTree(10, '*');

printChristmasTree(6, '#');

輸出


10|       *       |15=7+1+7

 9|      ***      |15=6+3+6

 8|     *****     |15=5+5+5

 7|    *******    |15=4+7+4

 6|   *********   |15=3+9+3

 5|  ***********  |15=2+11+2

 4| ************* |15=1+13+1

 3|***************|15=0+15+0

 2|      ***      |15=6+3+6

 1|      ***      |15=6+3+6

 6|   #   |7=3+1+3

 5|  ###  |7=2+3+2

 4| ##### |7=1+5+1

 3|#######|7=0+7+0

 2|  ###  |7=2+3+2

 1|  ###  |7=2+3+2

更新


這是height/4 (rounded)2的樹干高度的邏輯,而不是上面代碼使用的固定高度。樹干寬度仍固定為 3。


public static void printChristmasTree(int height, char ch) {

    final int trunkHeight = (height + 2) / 4; // rounded

    final int treeWidth = (height - trunkHeight) * 2 - 1;

    final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);

    for (int row = height; row > 0; row--) {

        int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);

        int spaces = (width - fill) / 2;

        System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,

                          repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),

                          spaces * 2 + fill, spaces, fill, spaces);

    }

}

測試


printChristmasTree(5, '*');

printChristmasTree(4, '*');

printChristmasTree(3, '*');

printChristmasTree(2, '*');

printChristmasTree(1, '*');

輸出


 5|   *   |7=3+1+3

 4|  ***  |7=2+3+2

 3| ***** |7=1+5+1

 2|*******|7=0+7+0

 1|  ***  |7=2+3+2

 4|  *  |5=2+1+2

 3| *** |5=1+3+1

 2|*****|5=0+5+0

 1| *** |5=1+3+1

 3| * |3=1+1+1

 2|***|3=0+3+0

 1|***|3=0+3+0

 2| * |3=1+1+1

 1|***|3=0+3+0

 1|*|1=0+1+0


查看完整回答
反對 回復 2022-01-19
  • 1 回答
  • 0 關注
  • 247 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號