Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何连接相邻的两个嵌套循环的输出?_Java_For Loop_Nested_Ascii Art - Fatal编程技术网

Java 如何连接相邻的两个嵌套循环的输出?

Java 如何连接相邻的两个嵌套循环的输出?,java,for-loop,nested,ascii-art,Java,For Loop,Nested,Ascii Art,所以我目前正在做一个个人项目,我制作了一个程序,可以打印出星图。在其中一个星形图案上,我需要以下输出: 身材 所以我用一种方法打印了这个: 图1 另一种方法是打印: 图2 我的问题:如何将第一个方法星放置在其他方法星的旁边 到目前为止,我得到的是: 公共静态无效打印StarsVictoryInt n{ 对于int i=1;i那么,System.out.println只打印到下一行,而不是右边。唯一的方法是创建一个新的算法。您应该将所有内容放入单个循环中 // loop starts from 0

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

身材

所以我用一种方法打印了这个:

图1

另一种方法是打印:

图2

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

到目前为止,我得到的是:

公共静态无效打印StarsVictoryInt n{
对于int i=1;i那么,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();
}

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();
}

每一行的星数不断增加,空间数不断减少

e、 g.对于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--;
    }
}

每一行的星数不断增加,空间数不断减少

e、 g.对于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);
    }
}

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

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();
    }
}
示例用法printStarsVictory12:


我认为您的思路是正确的,您只需要将两个程序合并到内部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();
    }
}
示例用法printStarsVictory12:

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

m[i][j]=Math.absi-Math.absj; 如果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
int n=4; IntStream.range-n,0 .mapMath::abs .peeki->IntStream.range关闭-n,n .mapMath::abs .mapToObjj->i>j?:* .forEachSystem.out::打印 .forEachi->System.out.println; 输出:

*               * 
* *           * * 
* * *       * * * 
* * * *   * * * * 
* * * * * * * * * 
另请参见:

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

m[i][j]=Math.absi-Math.absj; 如果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
int n=4; IntStream.range-n,0 .mapMath::abs .peeki->IntStream.range关闭-n,n .mapMath::abs .mapToObjj->i>j?:* .forEachSystem.out::打印 .forEachi->System.out.println; 输出:

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

另请参见:

您的答案不够清晰。您可以编辑它并使其清晰吗?例如,添加示例,显示代码等。它应该会更好,我也添加了一些注释。这是我第一次看到它…这不会产生预期的结果,无论是偶数还是正数。您的答案不够清晰。您可以编辑我吗t并说明清楚?例如,添加示例,显示代码等。如果它工作得更好,我还添加了一些注释。这是我第一次看到它……这不会产生偶数或正n的预期输出。
  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
*               * 
* *           * * 
* * *       * * * 
* * * *   * * * * 
* * * * * * * * *