卡码java基础课 | 9.打印正方形

学习内容:
通过一道题目来学习使用循环嵌套。

例题:

解:

点击查看代码
import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        for(int row = 0; row < n; row++){
            for(int col = 0; col < n; col++){
                if(row == 0 || col == 0 || row == n - 1 || col == n - 1){
                    System.out.print("*");
                }
                else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        } 
        
        sc.close();
    }
}
posted @ 2024-03-06 21:47  小陈-自学记录  阅读(26)  评论(0)    收藏  举报