简体   繁体   中英

2D Checker Board Java array not printing right

I have this for my code:

class CheckerBoard
{
   public static void main(String[] args)
   {
   int[][] board = new int[8][8];
   int r = 0;
   int c = 0;
   int i = 0;
   for(r = 0; r < 7; r++){
      for(c = 0; c < 7; c++)
         if((i % 2) == 0)
            board[r][c] = 'B';
         if((i % 2) != 0)
            board[r][c] = 'W';
         i++;
      }
      System.out.print(board[r][c]);                 
   }
}

Right now it prints out a single 0. Am I printing this 2d array wrong? the output should be:

BWBWBWBW

WBWBWBWB

BWBWBWBW

WBWBWBWB

BWBWBWBW

WBWBWBWB

BWBWBWBW

WBWBWBWB

Should I be adding loops for printing a 2d array? Appreciate any tips or help thanks.

You should use board.length as it is a property of arrays in java sine they are objects. Also, your for loops will not go all the way up to the full 8 elements in the array because you do less than 7 which means 0-6 which is 7 elements not 8.

There's two things wrong.

  • the print statement should be inside the second for loop. In the place you have it now, you should have an empty println , to print a line break

  • Your i++ counter is wrong. The first line of the chessboard ends with white and the second continues with white. With i++ you lose that. You could remove i altogether. A sufficient condition is: if (r + c) % 2 == 0 it's white, else black.

  1. Use char type instead of int to represent the matrix
  2. Brackets on the second for are needed
  3. You need to add a break line to jump between rows (System.out.println()). Also add space " " on the first print if you want B and W be separated
  4. (Style) You can initialise variables r and c on the for scope

     char[][] board = new char[8][8]; int i = 0; for(int r = 0; r < board.length; r++){ for(int c = 0; c < board[r].length; c++) { if (i % 2 == 0) board[r][c] = 'B'; else board[r][c] = 'W'; i++; System.out.print(board[r][c] + " "); } System.out.println(); }

There are a few issues going on here.

  1. You need a char array rather than an int array. (Technically, if you're just printing, you don't need an array at all, but I'm assuming the prints are for debugging and you actually want the structure).

  2. Your count only runs to 7 when it should run to 8. Using board.length and board[r].length pretty much removes having to think about this and is best practice.

  3. You only need 2 variables (rows and columns), so I'd recommend removing misleading and superfluous variables whenever possible.

  4. Your for needs brackets to execute more than 1 line of code in a block. Using brackets always means you'll never run into trouble.

  5. Your parity check can be simplified. You want to alternate every column and offset it by the parity of the row. This results in one conditional test: (r + c) % 2 == 0 .

Putting it all together:

class CheckerBoard {
    public static void main(String[] args) {
        char[][] board = new char[8][8]; // <-- char, not int

        for (int r = 0; r < board.length; r++) { // <-- iterate up to 8, not 7
            for (int c = 0; c < board[r].length; c++) { // <-- use braces
                if ((r + c) % 2 == 0) { // <-- include column in parity check
                    board[r][c] = 'B';
                }
                else {
                    board[r][c] = 'W';
                }

                System.out.print(board[r][c]);   
            }
            
            System.out.println();
        }
    }
}

Output:

BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM