简体   繁体   English

使用星型Java打印星

[英]Print star using star pattern Java

I want to print the below pattern 我想打印下面的图案

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

I am able to print the two outer pyramids in the pattern using the below code: 我可以使用下面的代码在模式中打印两个外部金字塔:

    public static void main(String[] args) {
    int n = 8;
    int i, j;

    for (i = 1; i <= n; i++) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        System.out.print("\n");

    }
    for (i = n; i >= 1; i--) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
}

This prints the pattern as 这会将图案打印为

*               
      * *              
     *   *             
    *     *             
   *       *           
  *         *              
 *           *
***************
***************
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *``

But how to combine these two to form a star shape? 但如何将这两者结合起来形成星形?

Since you didn't specify what your restrictions were... 既然你没有说明你的限制是什么......

public static void main(String[] args) {
    System.out.println("       *       ");
    System.out.println("***************");
    System.out.println(" *   *   *   * ");
    System.out.println("  * *     * *  ");
    System.out.println("   *       *   ");
    System.out.println("  * *     * *  ");
    System.out.println(" *   *   *   * ");
    System.out.println("***************");
    System.out.println("       *       ");
}

Now obviously that is not what you intend to do, but next time it would be of help if you are more specific about your requirements, ie: " I have to do it using only for loops ", " I'm not allowed to print more than a character at a time ", " the code must be able to print any star of a given width ", etc. 现在很明显这不是你打算做的,但下次如果你对你的要求更具体一点会有所帮助,即:“ 我必须只使用for循环 ”,“ 我不允许打印一次不止一个字符 “,” 代码必须能够打印给定宽度的任何星形 “,等等。
Because if none of those restrictions are in place, then the best way is just to print the ASCII lines like above. 因为如果没有这些限制,那么最好的方法就是打印如上所述的ASCII行。

You could temporarily store the printing in a 2D char array and modify it twice, then use a for loop to print the array. 您可以暂时将打印存储在2D char数组中并修改两次,然后使用for循环打印数组。

public static void main(String[] args) {
int n = 8;
char[][] temp = new char[][];
Arrays.fill(temp, ' ');
int i, j;

for (i = 1; i <= n; i++) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}
for (i = n; i >= 1; i--) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}

for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
        System.out.print(temp[i][j]);
    System.out.println();
}

} }

It seems to me that your loops are the same, except that one code block is ascending and one code block is descending. 在我看来,你的循环是相同的,除了一个代码块是升序和一个代码块下降。 You could probably combine the twp code blocks, and use a third integer, k, to handle the other pattern. 您可以组合twp代码块,并使用第三个整数k来处理其他模式。

public static void main(String[] args) {
    int n = 8;
    int i, j, k;

    for (i = 1; i <= n; i++) {
        k = n - i;

        for (j = i; j < n; j++) {
            if (/* TODO: handle k case */) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else if (/* TODO: handle k case */) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        System.out.print("\n");
    }
}

Make sense? 合理?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM