If I understood the problem correctly, I would propose this solution:
- conceptually, you need two functions:
- one to initialise floor an ceiling
---------------
static void initialiseCeilingAndFloor(char plane[][], int n) {
for (int i = 0; i < plane.length; i++) {
plane[0][i] = '-'; // ceiling is initialised
plane[plane.length - 1][i] = '-'; //floor is initialised
}
}
The other one to draw one line of each wall |. . . . . . . . .|.
Here I actually broke this method in two, for clarity: InitialiseInternalRow does what it says, and InitialiseBody uses it to initialise the whole body of the plane
static void initialiseBody(char plane[][]) {
// we already initialised floor and ceiling, so here we'll
for (int i = 1; i < plane.length - 1; i++) {
// reduce the cycle at the internal lines
initialiseInternalRow(plane, i);
}
}
static void initialiseInternalRow(char plane[][], int i) {
for (int j = 0; j < plane.length; j++) {
// if we are at the extremes, we draw a wall: '|'
if (j == 0 || j == plane.length - 1)
plane[i][j] = '|';
else // otherwise we leave space
plane[i][j] = ' ';
}
}
So that you can easily initialise your array in your main:
char plane[][] = new char[n][n];
initialiseCeilingAndFloor(plane);
initialiseBody(plane);
You could even think to wrap the two methods together to initialise the thing in one hit, like this:
static void initialisePlane(char plane[][]) {
initialiseCeilingAndFloor(plane);
initialiseBody(plane);
}
So that you could easily call from your main:
char plane[][] = new char[n][n];
initialisePlane(plane);
No worries if your terminal displays the output more as a rectangle. this depends on the formatting used by the particular application you are using to display the text. just replace the space with dots, and count them.
Serious testings here.