/** * @author WanJl * @version 1.0 * @title Demo07 * @description 课堂练习: 使用嵌套 for 循环和 if 语句打印一个 5 行 5 列的空心矩形星号图案(第一行和最后一行全满,第一列和最后一列全满,其余为空格)。 * @create 2026/7/15 */ public class Demo07 { public static void main(String[] args) { for (int i = 1; i <=5; i++) { //外层循环控制行 for (int j = 1; j <=5; j++) { //内层循环控制一行的中的列 //先研究 第1行 和第5行 if (i==1||i==5){ System.out.print("*"); }else { //第2、3、4行 if (j==1||j==5){ // j 第1列,或第5列 输出星号* System.out.print("*"); }else { //第2、3、4列,输出空格 System.out.print(" "); } } } //内层循环和外层循环之间输出一个换行符 System.out.println(); } } }