我画了一个假的圣诞树(java第一版)

简单思路

用*表示树冠

1、第一层一个、第二层三个、第三层五个,也就是说每层都会在他的上一层加两个星星

2、第一层的星星在中间。也就是第一层横线最多。第二层比第一层横线少一。思路有了接下来是代码。

3、盆的话  大家可以自由发挥。

想想中的

          *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
        *
        *
        *
        *
        *
  【----*----】
  【----*----】
  【---------】
   
   
   
   
----------*
---------***
--------*****
-------*******
------*********
-----***********
----*************
---***************
--*****************
-*******************
*********************
          *
          *
          *
          *
          *

/**
 * 星星
 * 星星每层加2
 * 是在上一层的基础上加2
 * 第一层一个 第二层3个
 * 第三层5个
 * 依次类推 取值都为奇数 看起来圣诞树会有层次感
 *
 * @param in
 * @return
 */
public static String starsSum(int in, String strStars) {
    String tempStars = "";
    in = in + 2;
    for (int i = 0; i < in; i++) {
        tempStars = tempStars + strStars;
    }
    return tempStars;
}

/**
 * 标尺
 * 标尺每层减一
 * 标尺第一层就是最多的一层
 * 依次递减
 *
 * @param inCount 第几层
 * @return
 */
public static String symbolsSum(int inCount, String strRuler) {
    String tempSymbols = "";
    for (int i = 0; i < inCount; i++) {
        tempSymbols = tempSymbols + strRuler;
    }
    return tempSymbols;
}
/**
 * 画树干
 *
 * @param inLayer 圣诞树树杉行数
 * @param inRow   树干有多高
 * @param strType 树干类型(样式)
 * @return
 */
public static String drawTrunk(int inLayer, int inRow, String strType) {
    String strNull = "";
    for (int i = 0; i < inLayer; i++) {
        strNull = strNull + " ";
    }
    StringBuffer sbTrunk = new StringBuffer();
    for (int i = 0; i < inRow; i++) {
        sbTrunk.append(strNull).append(strType).append("\n");
        System.out.println(strNull + strType);
    }
    return sbTrunk.toString();
}

 /**
     * 开始画圣诞树
     *
     * @param inLayer  圣诞数层数
     * @param inRow    圣诞树干多高
     * @param strType  圣诞树样式    *
     * @param strRuler 标尺- 也可以空格
     * @return
     */
    public static String drawChristmasTree(int inLayer, int inRow, String strType, String strRuler) {
        //标尺
        int tempIn = inLayer;
        //记录每行的星星
        String tempStars = "";
        String strBasin = "";
        //返回的一个圣诞树
        StringBuffer sbChristmasTree = new StringBuffer();
        for (int i = 0; i < inLayer; i++) {
            String strSymbols = symbolsSum(tempIn--, strRuler);
            System.out.print(strSymbols);
            sbChristmasTree.append(strSymbols);
            //第一层 画一个星星
            if (i == 0) {
                System.out.print(strType);
                sbChristmasTree.append(strType).append("\n");
                System.out.println();
                tempStars = strType;
            } else {
                //当前行包含多少个*
                int i1 = StringUtils.countMatches(tempStars, strType);
                tempStars = starsSum(i1, strType);
                System.out.println(starsSum(i1, strType));
                sbChristmasTree.append(tempStars).append("\n");
            }
        }
        //最后一行补充
        int i = StringUtils.countMatches(tempStars, strType);
        tempStars = starsSum(i, strType);
        System.out.println(starsSum(i, strType));
        sbChristmasTree.append(tempStars).append("\n");
        //树干
        String strTrunk = drawTrunk(inLayer, inRow, strType);
        sbChristmasTree.append(strTrunk).append("\n");
        return sbChristmasTree.toString();
    }

public static void main(String[] args) {
    drawChristmasTree(10, 5, "*", "-");
}

写的不好  希望大家多多指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一百减一是零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值