The natural structure in Java to implement a square board is a (square) two dimensional array:
char board[][] = new char[10][10];
I'm using char here, but you can use int or String or anything other type you like for the array element.
The usual interpretation is that board[i] represents row i (or i+1 in your case since you start numbering at 1, but array index values always start at 0 for the first element) and that board[i][j] then contains the value for row i, column j.
The i and j index names are math style, which I use a lot. For a board, though, I usually use row and col instead. Feel free to spell out "column" if it gets you a better grade.
You can loop for row=1 to 10 and adjust for 0-based indexing when accessing board[][], or you can use 0-based indexing for row and adjust when printing out values. I like the second approach.
I also like using the lengths stored in the board array to control loops, so I don't have to mess with changing a bunch of 10s to 8 or 12 if the board size changes. For that:
board.length = number of rows in the board
board[row].length = number of columns in row (row+1)
Yes, each row can be a different length. They won't be different if you use new char[10][10], but there are other ways to allocate and initialize an array.
Processing the whole board is nearly always done with nested loops, like this loop to initialize the board:
for (int row=0; row
.... for (int col=0; col<=board[row].length; ++col) {
.... .... board[row][col] = '~';
.... }
}
That loads the board with tildes. The .... tokens are for visual indentation. Pretend they're spaces.
The outer loop repeats once for each row. The inner loop repeats once for each column for a given row. I used the
The printout uses the same kind of loop, but with a few extra wrinkles for formatting. First, though, you need a heading that will line up with the board output in the loops. You could write out a single string, but I like to use the size of the board to drive this loop. What I will do is allow 2 positions for each row number so that spacing doesn't get messed up with 10+ rows. Ditto for columns, of course.
System.out.print(" "); // 4 space in quotes!
for (int col=0; col
.... System.out.printf(" %2d", col+1);
}
System.out.println();
That prints out the header line, with printf instead of print used to format column numbers with a space in front and 2 positions for the column number. That looks much line what will be done on each row, but the leading 4 spaces will be a row number, a space, and a "|" or other separator:
for (int row=0; row
.... System.out.printf("%2d |", row+1); // print row number 1-10
.... for (int col=0; col
.... .... System.out.printf(" %2s", board[row][col]);
.... }
.... System.out.println(); // end the output line after whole row is printed
}
That's just entered on-the-run, so expect a typo or two. You'll probably have something else for your array type and slightly different formatting, but this should get you to SOME kind of output that you can adjust to taste.