为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 269 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号