Reputation: 21
I have been working on this all week. Yes, it is homework, and I realize this same thing has been somewhat asked before but I cant make the solutions work for my specific code. I need to make a two dimensional array have a display that is 3x3
. I have to use the array, it is part of the assignment, and it must be 3x3
and have a print
method, so these are not changes I can make.
Right now they are printing out in one single line. Here is the result I am getting:
If you cant see the screenshot, the result I was getting was:
First Tic Tac Toe:
X O X O O X X X O
Second Tic Tac Toe:
O O X O O X X X O
Here is my code:
public class tictactoe {
public static void main(String[] args) {
System.out.println("First Tic Tac Toe: ");
char[][] ttt1 = { { 'X', 'O', 'X' }, { '0', 'O', 'X' }, { 'X', 'X', 'O' } };
print(ttt1);
System.out.println("Second Tic Tac Toe: ");
char[][] ttt2 = { { 'O', 'O', 'X' }, { '0', 'O', 'X' }, { 'X', 'X', 'O' } };
print(ttt2);
}
public static void print(char ttt1[][]) {
for (char row = 0; row < ttt1.length; row++) {
for (char column = 0; column < ttt1[row].length; column++)
System.out.print(ttt1[row][column] + " ");
}
System.out.println();
}
}
Upvotes: 2
Views: 1352
Reputation: 1
If you don't mind to see square brackets around each row and commas between each number in a row you can do:
java.util.Arrays;
for (int row = 0; row < ttt1.length; row++)
System.out.println(Arrays.toString(ttt1[row]));
Upvotes: 0
Reputation: 381
You are missing a new line once you print out each row.
public static void print(char ttt1[][]) {
for (char row = 0; row < ttt1.length; row++) {
for (char column = 0; column < ttt1[row].length; column++)
System.out.print(ttt1[row][column] + " ");
System.out.println();
}
System.out.println();
}
Upvotes: 2