如何将 2 个嵌套循环的输出连接在一起?

How to join the output of 2 nested loops next to each other?

所以我目前正在做一个个人项目,我制作了一个打印星形图案的程序。在其中一个星形图案上,我想要这个输出:

*        *
**      **
***    ***
****  ****
**********

所以我用一种方法打印出来:

图一

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("*" + " ");
    }
    System.out.println("");
}
*
**
***
****
*****

另一种方法打印出来:

图2

for (int i = 0; i < n; i++) {
    for (int j = 2 * (n - i); j >= 0; j--) {
        System.out.print(" ");
    }
    for (int j = 0; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}
    *
   **
  ***
 ****
*****

我的问题:如何将第一个方法的星星放在其他方法的星星旁边?

这是我目前得到的:

public static void printStarsVictory(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print("*" + " ");
        }
        System.out.println("");
    }
    for (int i = 0; i < n; i++) {
        for (int j = 2 * (n - i); j >= 0; j--) {
            System.out.print(" ");
        }
        for (int j = 0; j <= i; j++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}

这是目前正在打印的内容:

*
**
***
****
*****
    *
   **
  ***
 ****
*****

知道如何解决这个问题吗?

嗯,System.out.println() 只打印到下一行,而不是右边。唯一的方法是创建一个新算法。你应该把所有东西都放在一个循环中。

// loop starts from 0 and user counts from 1, so we wil decrement it by 1
n--;

// this loops moves pointer vertically
for (int i = 0; i < n; i++) {

    // this loops moves pointer horisontally
    for (int j = 0; j < n*2; j++) {

        // here we choose what to print
        if (j <= i || j >= n*2-i) {
            System.out.print("*");
        } else System.out.print(" ");
    }
    System.out.println();
}

每一行的星数递增,空格数递减。

例如对于 n=5 第一行每边各有 2 个星和 8 个空格。

在每次迭代中,您可以增加星号并减少空格并将它们打印在同一行上:

public static void printStarsVictory(int n) {

    int sp = n * 2 - 2; // number of spaces

    for (int i = 1; i <= n; i++) {
        printStars(i); // print stars on the left side
        int temp = sp;
        while (temp > 0) {
            System.out.print(" ");
            temp--;
        }
        printStars(i); // // print stars on the right side
        System.out.println(""); 
        sp -= 2; // decrease spaces on each side 
    }
}

public static void printStars(int i) {
    while(i>0) {
        System.out.print("*");
        i--;
    }
}

能否在打印输出之前将它们保存在一个数组中:

public static void printStarsVictory(int n) {
    StringBuilder[] lines = new StringBuilder[n + 1];

    for (int i = 0; i < n; i++) {
        lines[i] = new StringBuilder();
        for (int j = 0; j <= i; j++) {
            lines[i].append("*" + " ");
        }

        for (int j = 2 * (n - i - 1); j > 0; j--) {
            lines[i].append(" ");
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 2 * (n - i - 1); j > 0; j--) {
            lines[i].append(" ");
        }
        for (int j = 0; j <= i; j++) {
            lines[i].append("* ");
        }
    }

    for (StringBuilder line : lines) {
        System.out.println(line);
    }
}

我认为你走在正确的轨道上你只需要将你的两个程序组合到内部 for 循环中:

private static void printStarsVictory(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < n * 2; j++) {
            if (j <= i || j >= (n * 2 - i)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

用法示例printStarsVictory(5):

*       *
**     **
***   ***
**** ****
*********

用法示例printStarsVictory(12):

*                     *
**                   **
***                 ***
****               ****
*****             *****
******           ******
*******         *******
********       ********
*********     *********
**********   **********
*********** ***********
***********************

您可以将此 可视化为一个范围内的数字矩阵:垂直 [-n, 0] 和水平 [-n, n] 包括,其中每个点是:

m[i][j] = Math.abs(i) - Math.abs(j);

如果n = 4,那么这个矩阵看起来像这样:

  0  1  2  3  4  3  2  1  0
 -1  0  1  2  3  2  1  0 -1
 -2 -1  0  1  2  1  0 -1 -2
 -3 -2 -1  0  1  0 -1 -2 -3
 -4 -3 -2 -1  0 -1 -2 -3 -4

Try it online!

int n = 4;
IntStream.rangeClosed(-n, 0)
        .map(Math::abs)
        .peek(i -> IntStream.rangeClosed(-n, n)
                .map(Math::abs)
                .mapToObj(j -> i > j ? "  " : "* ")
                .forEach(System.out::print))
        .forEach(i -> System.out.println());

输出:

*               * 
* *           * * 
* * *       * * * 
* * * *   * * * * 
* * * * * * * * * 

另请参阅:Making an hourglass using asterisks in java

类似于这个,你可以用同样的方式使用两个嵌套的for循环和一个if else语句 :

public static void main(String[] args) {
    int n = 9;
    for (int i = -n; i <= 0; i++) {
        for (int j = -n; j <= n; j++)
            if (Math.abs(i) <= Math.abs(j)
                    // in chessboard order
                    && (i + j) % 2 == 0
                    // vertical borders
                    || Math.abs(j) == n)
                System.out.print("*");
            else
                System.out.print(" ");
        System.out.println();
    }
}

输出:

*                 *
**               **
* *             * *
** *           * **
* * *         * * *
** * *       * * **
* * * *     * * * *
** * * *   * * * **
* * * * * * * * * *
** * * * * * * * **

另请参阅:
How to print a given diamond pattern in Java?