Java 简单的圣诞树

我是 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 *OMEGALUL

10|         *

 9|        ***

 8|       *****

 7|      *******

 6|     *********

 5|    ***********

 4|   *************

 3|  ***************

 2| *****************

 1|*******************

如何添加树干,它总是 3 个字母长,每棵树的 1/4 大。(四舍五入)和每行末尾的 |15=7+1+7,其中包含: 树的宽度为左侧空格的总和 + 相应行中树的宽度 + 右侧的空格(左对齐)。


郎朗坤
浏览 270回答 1
1回答

烙印99

这是一种不同的方法,它计算一行所需的字符数spaces和数量,然后用于打印行。fillprintfpublic static void printChristmasTree(int height, char ch) {&nbsp; &nbsp; if (height <= 4)&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Height must be 5 or higher");&nbsp; &nbsp; for (int row = height; row > 0; row--) {&nbsp; &nbsp; &nbsp; &nbsp; int spaces = (row > 2 ? row - 3 : height - 4);&nbsp; &nbsp; &nbsp; &nbsp; int fill = (height - spaces) * 2 - 5;&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces * 2 + fill, spaces, fill, spaces);&nbsp; &nbsp; }}private static String repeat(int count, char ch) {&nbsp; &nbsp; char[] buf = new char[count];&nbsp; &nbsp; java.util.Arrays.fill(buf, ch);&nbsp; &nbsp; return new String(buf);}测试printChristmasTree(10, '*');printChristmasTree(6, '#');输出10|&nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp;|15=7+1+7&nbsp;9|&nbsp; &nbsp; &nbsp; ***&nbsp; &nbsp; &nbsp; |15=6+3+6&nbsp;8|&nbsp; &nbsp; &nbsp;*****&nbsp; &nbsp; &nbsp;|15=5+5+5&nbsp;7|&nbsp; &nbsp; *******&nbsp; &nbsp; |15=4+7+4&nbsp;6|&nbsp; &nbsp;*********&nbsp; &nbsp;|15=3+9+3&nbsp;5|&nbsp; ***********&nbsp; |15=2+11+2&nbsp;4| ************* |15=1+13+1&nbsp;3|***************|15=0+15+0&nbsp;2|&nbsp; &nbsp; &nbsp; ***&nbsp; &nbsp; &nbsp; |15=6+3+6&nbsp;1|&nbsp; &nbsp; &nbsp; ***&nbsp; &nbsp; &nbsp; |15=6+3+6&nbsp;6|&nbsp; &nbsp;#&nbsp; &nbsp;|7=3+1+3&nbsp;5|&nbsp; ###&nbsp; |7=2+3+2&nbsp;4| ##### |7=1+5+1&nbsp;3|#######|7=0+7+0&nbsp;2|&nbsp; ###&nbsp; |7=2+3+2&nbsp;1|&nbsp; ###&nbsp; |7=2+3+2更新这是height/4 (rounded)2的树干高度的逻辑,而不是上面代码使用的固定高度。树干宽度仍固定为 3。public static void printChristmasTree(int height, char ch) {&nbsp; &nbsp; final int trunkHeight = (height + 2) / 4; // rounded&nbsp; &nbsp; final int treeWidth = (height - trunkHeight) * 2 - 1;&nbsp; &nbsp; final int width = (treeWidth > 3 || trunkHeight == 0 ? treeWidth : 3);&nbsp; &nbsp; for (int row = height; row > 0; row--) {&nbsp; &nbsp; &nbsp; &nbsp; int fill = (row > trunkHeight ? (height - row) * 2 + 1 : 3);&nbsp; &nbsp; &nbsp; &nbsp; int spaces = (width - fill) / 2;&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%2d|%s%s%s|%d=%d+%d+%d%n", row,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat(spaces, ' '), repeat(fill, ch), repeat(spaces, ' '),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces * 2 + fill, spaces, fill, spaces);&nbsp; &nbsp; }}测试printChristmasTree(5, '*');printChristmasTree(4, '*');printChristmasTree(3, '*');printChristmasTree(2, '*');printChristmasTree(1, '*');输出&nbsp;5|&nbsp; &nbsp;*&nbsp; &nbsp;|7=3+1+3&nbsp;4|&nbsp; ***&nbsp; |7=2+3+2&nbsp;3| ***** |7=1+5+1&nbsp;2|*******|7=0+7+0&nbsp;1|&nbsp; ***&nbsp; |7=2+3+2&nbsp;4|&nbsp; *&nbsp; |5=2+1+2&nbsp;3| *** |5=1+3+1&nbsp;2|*****|5=0+5+0&nbsp;1| *** |5=1+3+1&nbsp;3| * |3=1+1+1&nbsp;2|***|3=0+3+0&nbsp;1|***|3=0+3+0&nbsp;2| * |3=1+1+1&nbsp;1|***|3=0+3+0&nbsp;1|*|1=0+1+0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java