java圣诞树

Java圣诞树🎄

主类代码

package cn.qkmango.festival;

import java.util.ArrayList;

/**
 * @author qkmango
 * @version 1.0
 * @homepage http://www.qkmango.cn
 * 圣诞树🎄,生成圣诞树并打印
 * @date 2021-12-17 15:18
 */
class TreeObject {
    private ArrayList<Node> nodes = new ArrayList<>();
    private int maxWidth = 0;

    /**
     * 添加圣诞树的节点
     * @param topWidth 节点顶部宽度
     * @param bottomWidth 节点底部宽度
     * @param height 节点高度
     * @return 当前对象
     */
    public TreeObject addNode(int topWidth, int bottomWidth, int height) {
        Node node = new Node(topWidth, bottomWidth, height);
        if (node.getMaxWidth() > maxWidth) {
            maxWidth = node.getMaxWidth();
        }
        nodes.add(node);
        return this;
    }

    /**
     * 打印圣诞树
     */
    public void printTree() {
        for (Node node : nodes) {
            int topWidth = node.getTopWidth();
            int bottomWidth = node.getBottomWidth();
            int height = node.getHeight();

            //每层左侧增量
            int leftIncrement = (int) ((float) (bottomWidth - topWidth) / 2 + 1) / height;

            for (int i = 0; i < height; i++) {
                int symbolCount = (topWidth + (i * leftIncrement)) * 2 - 1;
                int spaceCount = (maxWidth - symbolCount) / 2;

                printSpace(spaceCount);
                printSymbol(symbolCount);
                System.out.println();
            }
        }
    }

    /**
     * 打印空格
     * @param num 打印的数量
     */
    private void printSpace(int num) {
        for (int i = 0; i < num; i++) {
            System.out.print(" ");
        }
    }

    /**
     * 打印符号
     * @param num 打印的数量
     */
    private void printSymbol(int num) {
        for (int i = 0; i < num; i++) {
            System.out.print("@");
        }
    }

    /**
     * 获取圣诞树所有节点中最大的宽度
     * @return 节点最大宽度
     */
    private int getMaxWidth() {
        return this.maxWidth;
    }

    /**
     * 圣诞树每个节点(圣诞树每层)
     */
    private class Node {
        private int topWidth;
        private int bottomWidth;
        private int height;

        public Node() {
        }

        public Node(int topWidth, int bottomWidth, int height) {
            this.topWidth = topWidth;
            this.bottomWidth = bottomWidth;
            this.height = height;
        }

        /**
         * 获取当前节点的宽/高最大值
         * @return 当前节点的宽高最大值
         */
        public int getMaxWidth() {
            return Math.max(topWidth, bottomWidth);
        }


        public int getTopWidth() {
            return topWidth;
        }

        public void setTopWidth(int topWidth) {
            this.topWidth = topWidth;
        }

        public int getBottomWidth() {
            return bottomWidth;
        }

        public void setBottomWidth(int bottomWidth) {
            this.bottomWidth = bottomWidth;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

    }
}

测试

public class Main {
    public static void main(String[] args) {
        new TreeObject()
                .addNode(1, 23, 4)
                .addNode(5, 41, 6)
                .addNode(9, 65, 9)
                .addNode(5, 5, 9)
                .printTree();
    }
}

效果

                                @
                             @@@@@@@
                          @@@@@@@@@@@@@
                       @@@@@@@@@@@@@@@@@@@
                            @@@@@@@@@
                         @@@@@@@@@@@@@@@
                      @@@@@@@@@@@@@@@@@@@@@
                   @@@@@@@@@@@@@@@@@@@@@@@@@@@
                @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
             @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        @@@@@@@@@@@@@@@@@
                     @@@@@@@@@@@@@@@@@@@@@@@
                  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@
               @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
                            @@@@@@@@@
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值