I have written this code so I know it works but I always seem to have difficulties creating a table so my numbers are evenly spaced. Can anyone help me with my last method PlayTour()
so when I run the program my table of numbers are evenly spaced and line up correctly?
public void playTour() {
System.out.printf("%n%n");
//display numbers in column
for (int a = 0; a < 8; a++)
System.out.printf(" %d", a);
System.out.printf("%n%n");
for (int row = 0; row < chessBoard[0].length; row++) {
System.out.printf("%d ", row);
for (int column = 0; column < chessBoard[1].length; column++)
System.out.printf(" %d", chessBoard[row][column]);
System.out.println();
}
}//end method playTour
You can use a length specifier when using printf.
Example:
for(int a = 0; a < 8; a++)
System.out.printf("%3d ",a);
will use 3 positions(like for a 3 digit number) for each number from 0 to 8, followed by a space.
I think this looks a bit more like you are expecting it to look like:
// display numbers in column
System.out.print(" ");
for(int a = 0; a < 8; a++) {
System.out.printf("%3d", a);
}
System.out.printf("%n");
for(int row = 0; row < chessBoard[0].length; row++) {
System.out.printf("%3d ", row);
for(int column = 0; column < chessBoard[1].length; column++) {
System.out.printf("%3d", chessBoard[row][column]);
}
System.out.println();
}
Output is something like this:
0 1 2 3 4 5 6 7
0 63 32 71 1 25 2 5 3
1 6 3 7 1 5 2 5 3
2 6 3 7 1 5 2 5 3
3 6 3 7 1 5 2 5 3
4 6 3 7 1 5 2 5 3
5 6 3 7 1 5 2 5 3
6 6 3 7 1 5 2 5 3
7 6 3 7 1 5 2 5 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.