user2988501
user2988501

Reputation: 107

Determining spacing in a grid, before and after a while loop and implementing it

I'm wondering if anyone point me in the right direction. I'm trying to figure out how I would go about designing this code:

private static void displaySeating(int s,int n) {

        int  i, k;

        i = 0;
        k = 0;
        while (i < s) {
            i++;
            System.out.print("\t X");
            if (i % 8 == 0) {
                System.out.println();
            }
        }

        while ( k < n) {
            k++;
            System.out.print("\t O");
            if ((k+s) % 8 == 0)  {
                System.out.println();
            }
        }

  }

So that it would fit in this grid, but with the open grid walls closed around it? With wick and wickre, being int variables that I have declared eg wick=8, so 8 x's and wickre=36, so 36 o's.

                System.out.println();
            System.out.println("#########################################################################"); 
            System.out.println("#           |                                                |          #"); 
            System.out.println("#           |   Drury Lane                                   |          #");  
            System.out.println("#           |   "+sh1+calcSpaces(45, sh1)+"|          #");
            System.out.println("#           |                                                |          #");
            System.out.println("#           --------------------------------------------------          #");
            System.out.println("#                                                                       #");
            System.out.println("#                                                                       #"); 
            displaySeating(wick, wickre);
            System.out.println();
            }

I'm hoping for something similar to this

With the ouput put looking like this

Upvotes: 0

Views: 53

Answers (1)

Octoshape
Octoshape

Reputation: 1141

Here's what you want:

private static void displaySeating(int s,int n) {

int  i, k;

    i = 0;
    k = 0;

    // print left edge
    System.out.print("#");

    while (i < s) {
        i++;
        System.out.print("\t X");
        if (i % 8 == 0) {
            // print right edge
            System.out.println("\t#");

            // print left edge of next row
            System.out.print("#");
        }
    }        

    while ( k < n) {
        k++;
        System.out.print("\t O");
        if ((k+s) % 8 == 0)  {
            // print right edge
            System.out.println("\t#");

            // print left edge of next row
            System.out.print("#");
        }
    }

    if ((s + n) % 8 != 0) {
        // this means the last row isn't completely full with 0's

        // fill up the row with tabs to get to the edge
        while ((k+s) % 8 != 0) {
            k++;
            System.out.print("\t");
        }
        // print the right edge
        System.out.println("\t#");

        // add the lower edge
        System.out.println("#########################################################################"); 
    } else {
        // this means the last row was filled completely but the left edge of 
        // the next row has already been printed, so we make the lower edge 1 smaller.
        System.out.println("########################################################################");
    }
}

EDIT

Found a bug: In case (s + n) % 8 == 0 we don't need to fill the last row up with tabs. Added a fix in the code above.

Upvotes: 1

Related Questions