使用三重嵌套 for 循环来增加打印行的长度和数量

Using a triple nested for loop to increase the length and count of the rows printed

我很好奇如何编写这样的代码:

##
##
####
####
####
####
######
######
######
######
######
######
########
########
########
########
########
########
########
########

这是我尝试过的:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i <= 8; i += 2) {
            for (int j = 0; j < i; j++) {
                System.out.print('#');
            }
            System.out.println();
        }
    }
}

这段代码显示:

##
####
######
########

但是它打印的行数没有多少个字符那么多。

所以基本上,它递增 2,然后显示与循环内字符数相同的行数。我想不通。

第三个嵌套 for 循环会是什么样子?

我会在你的两个循环之间使用另一个循环。

for (int i = 0; i <= 8; i += 2) {
    for (int k = 0; k < i; ++k) {
        for (int j = 0; j < i; j++) {
            System.out.print('#');
        }
        System.out.println();
    }
}

它打印:

##
##
####
####
####
####
######
######
######
######
######
######
########
########
########
########
########
########
########
########

我们再添加一个循环:

public static void main(String[] args) {
    for (int i = 0; i <= 8; i += 2)
        for (int j = 0; j < i; j++) {
            for (int k = 0; k < i; k++) {
                System.out.print('#');
            }
            System.out.println();
        }
}

您可以在第一个循环中使用两个 non-nested 循环。看这个例子:

public static void main(String[] args) {
    
    for (int i = 0; i <= 8; i += 2) {
        // use a StringBuilder to build up the line to be printed
        StringBuilder lineBuilder = new StringBuilder();
        // then use one loop to build the line with the desired amount of #
        for (int j = 0; j < i; j++) {
            lineBuilder.append('#');
        }
        // and then use an identical loop, but this time print the line
        for (int j = 0; j < i; j++) {
            System.out.println(lineBuilder.toString());
        }
    }
}

结果就是想要的结果(这里为了简洁省略)。

这可以通过一个循环完成,使用 String::repeat / Collections.nCopies 创建包含 N # 个字符的行,然后创建 N 行:

1. String::repeat
实例方法 String::repeat 自 JDK 11 于 2018 年 9 月发布后可用

for (int i = 2; i <= 8; i += 2) {
    String row = "#".repeat(i) + "\n";
    String rows = row.repeat(i);
    System.out.print(rows);
}

2。 String::join + Collections.nCopies

import static java.lang.String.join;
import static java.util.Collections.nCopies;

//...

for (int i = 2; i <= 8; i += 2) {
    String row = join("", nCopies(i, "#"));
    String rows = join("\n", nCopies(i, row));
    System.out.println(rows);
}

使用 String#replace 方法的单个 for 循环的分号解决方案 因为:1.5:

for (int i = 1; i < 5; i++)
    // print one line containing
    // newline symbols '\n'
    System.out.print(Collections
            // 'i * 2' copies of rows "##\n"
            // returns List<String>
            .nCopies(i * 2, Collections
                    // 'i * 2' copies of string "#"
                    // returns List<String>
                    .nCopies(i * 2, "#")
                    // List<String> to string
                    // [#, #]
                    .toString()
                    // #, #]
                    .replace("[", "")
                    // #, #
                    .replace("]", "")
                    // ##\n
                    .replace(", ", "") + "\n")
            // List<String> to string
            // [##\n, ##\n]
            .toString()
            // ##\n, ##\n]
            .replace("[", "")
            // ##\n, ##\n
            .replace("]", "")
            // ##\n##\n
            .replace(", ", ""));

使用 方法 双重嵌套 for 循环 的解决方案 因为: 1.8:

for (int i = 1; i < 5; i++) {
    for (int j = 0; j < i * 2; j++) {
        String[] arr = new String[i * 2];
        Arrays.fill(arr, "#");
        System.out.print(String.join("", arr));
        System.out.println();
    }
}

使用 双重嵌套 IntStream using String#repeat 方法的解决方案 因为:Java 11:

IntStream.range(1, 5)
        .forEach(i -> IntStream.range(0, i * 2)
                .forEach(j -> System.out.println("#".repeat(i * 2))));

输出:

##
##
####
####
####
####
######
######
######
######
######
######
########
########
########
########
########
########
########
########