-2

I want create a rectangle which has hole in the middle. How can I import wallThick :) I define width, height, wallThick but I just write a rectangle. I can't press any hole. Could you help me please... Thanks for all...

    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

1 Answers1

0

Simplest solution: calculate the start and end "coordinates" of the hole. If you are within the hole coordinates, print a blank space.

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

Check if you're within the hole using:

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

Sample code: here

Vasan
  • 4,810
  • 4
  • 20
  • 39