Java简易圣诞树

Java简易圣诞树,java,for-loop,Java,For Loop,我是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|***********

我是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();

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

这里有一种不同的方法,它计算一行上所需的
空格数和
填充
字符数,然后使用
printf
打印行

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

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
(四舍五入)的逻辑,而不是上面代码使用的固定高度
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);
    }
}
试验

输出

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

看来你走对了方向。也许只有一个高度循环,然后有numLeftSpaces和numRightSpaces变量,每次循环迭代都会减少这些变量。您可以为NuMasterisk使用另一个变量,该变量在每个循环迭代中递增两个。如果你能做到这一点,你应该能够把数字打印到右边。对于树干,它似乎相当简单,所以继续尝试。
trunkheath=treeheaght-(treeheaght*4)/5
然后
trunkheath=treeheaght-trunkheath
在您的示例中,如果总高度为10,那么第一位将计算树叶为8,树干为2。我想你可以从那里开始。就在现在,我真的很想让它工作,但很遗憾,我不能让它工作,我的最后期限是在几个小时。谢谢你的努力。工作完美,我只需要几分钟就可以完全理解代码,但它看起来比我的效率高很多。谢谢,我也投了更高的票,但没有显示,因为我只有不到15个代表。有一个问题:有没有办法让它也适用于高度在1到5之间的树?@FlazzerXP因为树的树干总是2x3,高度为1或2的树只会是树干,而高度为3或4的树看起来不像树,所以,任何高度它看起来都不像一棵树,但我的任务是,它必须适用于所有自然正数。树干是树高的1/4(圆形,但树干必须始终存在),而不是2。只是宽度总是3。我必须承认,作为初学者,我发现代码也很复杂,很难理解。使用for循环更容易解决吗?@FlazzerXP删除
if
语句,并更改计算逻辑以正确计算这些值的
空格和
填充仅供参考:
(高度/4)
不是圆的,而是截断的。
printChristmasTree(10, '*');
printChristmasTree(6, '#');
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, '*');