在 java 中打印矩形(中间有孔)

Printing rectangle in java (there is hole in center)

我想创建一个中间有孔的矩形。我如何导入 wallThick :) 我定义了宽度、高度、wallThick,但我只写了一个矩形。我不能按任何孔。你能帮帮我吗...谢谢大家...

    if (width <= 0 || height <= 0 || wallThick <= 0)
    {
      System.out.println("Invalid value! Please enter positive integer.");
    }else {
      for ( y = 1; y <= height; y++)
      {
        for(x = 1; x <= width; x++)
        {
          System.out.print("*");

        }
        System.out.println();
      }

what I want to do

最简单的解决方案:计算孔的起点和终点"coordinates"。如果您在孔坐标内,则打印空白 space。

int holeStartRow = wallThick + 1;
int holeStartCol = wallThick + 1;   
int holeEndRow = height - wallThick;
int holeEndCol = width - wallThick;

使用以下方法检查您是否在洞内:

if (y >= holeStartRow && y <= holeEndRow && x >=holeStartCol && x <= holeEndCol)

示例代码:here