(最简单的图形算法)用Java代码输出一个高度和底都可以通过参数控制的等腰三角形(允许不规则的等腰三角形)

用Java代码输出一个高度和底都可以通过参数控制的等腰三角形(允许不规则的等腰三角形)

最简单的(可伸缩变化的)图形算法

e.g:输入高度为9,长度为7的,实现不规则的等腰三角形

@GetMapping("/可变参等腰1")
public String changpar(int high,int bottom){//高度,最后一层*个数
    String results = "";
    if (bottom % 2 == 1){
        int firstbottom = 1;//第一层*个数
        float tolerance =(float)(bottom - firstbottom)/(high - 1); //公差 //传入的数据都是int类型时先计算再类型转换
        System.out.println(tolerance);
        for (int i = 0; i < high; i++){
            int quantitystar = (int)(firstbottom + (i) * tolerance);
            if (quantitystar % 2 == 0){
                quantitystar--;
            }
            int quantityspace = (bottom - quantitystar)/2;

            for (int j = 0; j < quantityspace; j++){
                results+= "&nbsp;&nbsp;";
            }
            for (int j = 0;j < quantitystar;j++){
                results+= '*';
            }
            results+= "<br/>";
        }

    }

    return results;
}

执行效果如下

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值