怎么才能形成一个空心矩形java

Scanner sc=new Scanner(System.in);
 System.out.println("请输入行数和列数");
 int x=sc.nextInt();
 int y=sc.nextInt();
 

 for(int i=0;ifor(int j=0;jif(i ==0) {
         System.out.print("*");
     }
       else if(i == y -1) {
         System.out.print("*");
     }
     else if(j == 0 || j == y-1) {
         System.out.print("*");
     }
     else {
         System.out.print("");
           }
 }
     System.out.println();
             }
   }

}




import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入行数和列数");
        int x = sc.nextInt();
        int y = sc.nextInt();

        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                if (i == 0 || i == y - 1) {
                    System.out.print("*");
                } else if (j == 0 || j == x - 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}




img


public class HollowRectangle {
    public static void main(String[] args) {
        int rows = 5; // 定义矩形的行数
        int columns = 10; // 定义矩形的列数

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= columns; j++) {
                if (i == 1 || i == rows || j == 1 || j == columns) {
                    System.out.print("*"); // 输出边框
                } else {
                    System.out.print(" "); // 输出空格
                }
            }
            System.out.println(); // 换行
        }
    }
}

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^