用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+= " ";
}
for (int j = 0;j < quantitystar;j++){
results+= '*';
}
results+= "<br/>";
}
}
return results;
}
执行效果如下