public class hollowPyramid { public static void main(String[] args){ int level = 15;//变量行数 for (int i = 1;i <= level;i++){//外层循环控制行数 //空格打印 for (int k = 1;k <= level - i;k++){//内层循环控制列数 System.out.print(" "); } //星星打印 判断哪些位置打印空格哪些位置打印星星 for (int j = 1;j <= 2*i-1;j ++){ if (j == 1 || j == 2*i-1 || i == level){ System.out.print("*"); }else{ System.out.print(" "); } } //用来换行,形成一行一行的效果 System.out.println(); } } }
运行结果展示: