I'm trying to display a array board for my tic-tac-toe game.
The board is supposed to look like this:
123
456
789
However when I run the program it shows up like this:
150
159
168
My Code:
class TicTacToe
{
public static void main(String[] args)
{
char [][] board = {{'1','2','3'}, {'4','5','6'}, {'7', '8', '9'}};
System.out.println(board[0][0] + board[0][1] + board[0][2]);
System.out.println(board[1][0] + board[1][1] + board[1][2]);
System.out.println(board[2][0] + board[2][1] + board[2][2]);
}
}
Those values you see are the addition/sum of the character in there decimal values in the ascii table:
'1' = 49
'2' = 50
'3' = 51
equals
150
What you need to do is add/append an empty string instead of adding the characters decimal value
System.out.println(board[0][0] +""+ board[0][1] + board[0][2]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With