Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display arrays

Tags:

java

arrays

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]);


   }
}
like image 219
Justin Beaubien Avatar asked Dec 25 '22 20:12

Justin Beaubien


1 Answers

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]);
like image 101
Rod_Algonquin Avatar answered Jan 06 '23 23:01

Rod_Algonquin