使用嵌套的 for 循环语句来绘制 " " 的三角形。最后一行的“ ”个数由用户输入(有效范围:5 到 21)。输出应如下所示: 示例输出:
多少颗星/最后一排 (5-21)?25 超出范围。重新进入:7
*
**
***
****
*****
******
*******
到目前为止,这就是我所拥有的代码。我不知道如何让它看起来像一个三角形。任何帮助都会很棒。
import java.util.Scanner;
public class Lab7_2{
public static void main(String[] args){
//declarations
Scanner input= new Scanner(System.in);
int how_many;//num of triangles
int m; //constant
int c;//constant
int rows;//row
//prompt for input
System.out.println("Drawing triangles program.");
System.out.println("==============================");
System.out.println("How many triangles?");
how_many=input.nextInt();
System.out.println("How many stars/last row (5-21)?");
rows=input.nextInt();
while(rows<=5||rows>=21){
System.out.println("Out of range. Reenter: ");
rows=input.nextInt();
}
for(m=1;m<=rows;m++){
for(c=1;c<=m;c++){
System.out.println("*");
System.out.println();
}
}
}
}