鉴于:

public static void printTriangle(int sideLength)
{
    for (int i = 0; i <= sideLength; i++) {
        for (int j = 0; j < i; j++){
            System.out.print("[]");
        }
        System.out.println();
    }
}


如何修改代码以打印sideLength = 3的正方形?

[][][]
[][][]
[][][]

最佳答案

像这样:

public static void printSquare(int sideLength)
{
    for (int i = 0; i < sideLength; i++) {
        for (int j = 0; j < sideLength; j++) {
            System.out.print("[]");
        }
        System.out.println();
    }
}

08-06 20:36