/**
* 空心金字塔
*/
public class Demo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入金字塔层数:");
int cell = scanner.nextInt();
//镂空两边部分
for (int i = 1; i <= cell; i++){
System.out.println("");
for (int j = 1; j <= 2 * cell - 1; j++){
if (j <= (cell - i)){ //左边第一个被镂空的位置
System.out.print(" ");
continue;
}else if (j >= (cell + i)){ //右边第一个被镂空的位置
System.out.print(" ");
continue;
}
//镂空中间部分
if(i >= 2 && i <= cell - 1){
if(j > (cell - i + 1) && j < (cell + i - 1)){
System.out.print(" ");
continue;
}
}
System.out.print(" △ ");
}
}
}
}
利用循坏嵌套打印空心金字塔
最新推荐文章于 2024-09-25 11:06:23 发布