简体   繁体   English

Java中的空心方形分割模式

[英]Hollow Square Split Pattern in Java

I'm trying to create a program where you input an integer, and its output are hollowed square cells.. example output are shown below.我正在尝试创建一个程序,您可以在其中输入一个整数,其输出是空心方形单元格……示例输出如下所示。

1 ↵ 1 ↵

*

2 ↵ 2 ↵

* * * 
* * * 
* * * 

3 ↵ 3 ↵

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

4 ↵ 4 ↵

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

And so on.等等。 Btw, here's where I am now.顺便说一句,这就是我现在所在的地方。

int input = 4; // sample input
int sides;

sideL = input * input - (input - 1);

for (int a = 1; a <= sides; a++) {
  for (int b = 1; b <= sides; b++) {
    if (a == 1                 ||
      a == sides               ||
      a == sides / input + 1   ||
      a == sides / input + input ||
      b == 1                   ||
      b == sides               ||
      b == sides / input + 1   ||
      b == sides / input + input)
    {
      System.out.print("* ");
    } else {
        System.out.print("  ");
    }
  }
  System.out.println();
}

and it output它输出

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

It continues with having 2 splits.它继续进行 2 次拆分。 What am I doing wrong?我究竟做错了什么?

Generally when you see multiple loops nested inside each other, you might think there's something wrong with the design.通常,当您看到多个循环相互嵌套时,您可能会认为设计有问题。 You can simplify the problem if you consider printing the big picture line-by-line.如果您考虑逐行打印大图,则可以简化问题。 You have 2 kinds of lines:你有两种线路:

  • Border line (made full of stars)边界线(由星星组成)
  • Inner line (which only has stars for the vertical columns)内线(只有垂直列的星星)

So the main function should look something like this:所以主函数应该是这样的:

void hollowSquares(int number) {
    // Default cases
    if (number < 1) {
        return;
    }
    if (number == 1) {
        System.out.println("*");
        return;
    }
    
    // how many lines will be actually printed
    int numberOfLines = number * (number - 1) + 1;
    
    for (int i = 0; i < numberOfLines; i++) {
        if (isBorderLine(i, number)) {
            printBorderLine(numberOfLines);
        } else {
            printInnerLine(number);
        }
    }   
}

boolean isBorderLine(int line, int number) {
    return line % (number - 1) == 0;
}

So the only thing left to do is the actual printing.所以剩下要做的就是实际打印。 For the simpler case, border line, you repeat the star-and-space pattern:对于更简单的情况,边界线,重复星和空间模式:

void printBorderLine(int numberOfLines) {
    String line = String.join("", Collections.nCopies(numberOfLines, "* "));
    System.out.println(line);
}

For the inner border, you want to repeat the start-and-a-couple-spaces pattern, followed by a trailing star对于内边框,您要重复开始和一对夫妇空间模式,然后是尾随星形

void printInnerLine(int number) {
    int numberOfEmptySpaces = 2 * number - 3;
    String emptySpaces = String.join("", Collections.nCopies(numberOfEmptySpaces, " "));
    String starWithSpacesPattern = "*" + emptySpaces;
    String line = String.join("", Collections.nCopies(number, starWithSpacesPattern));
    line += "*"; // trailing star
    System.out.println(line);
}

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

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