在线问题
- 请编写一个程序,可以接收一个整数,表示层数(totalLevel),
打印出空心金字塔。(使用for循环)
思路分析
- 打印矩形
- 打印半个金字塔
- 打印整个金字塔
- 打印空心金字塔
代码
public static void main(String[] args) {
int totalLevel = 10;//表示层数
for (int i = 1; i <= totalLevel; i++) { // i 表示层
// 输出空格
for (int k = 1; k <= totalLevel - i; k++) {
System.out.print(" ");
}
// 输出*
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == totalLevel) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}