简体   繁体   English

2D Checker Board Java 阵列打印不正确

[英]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?现在它打印出一个 0。我打印这个二维数组错了吗? the output should be:输出应该是:

BWBWBWBW BWBWBWBW

WBWBWBWB WBWBWBWB

BWBWBWBW BWBWBWBW

WBWBWBWB WBWBWBWB

BWBWBWBW BWBWBWBW

WBWBWBWB WBWBWBWB

BWBWBWBW BWBWBWBW

WBWBWBWB 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.您应该使用 board.length,因为它是 java 中数组的属性,因为它们是对象。 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.此外,您的 for 循环不会一直到达数组中的全部 8 个元素,因为您执行的次数少于 7,这意味着 0-6 是 7 个元素而不是 8 个。

There's two things wrong.有两件事不对。

  • the print statement should be inside the second for loop.打印语句应该在第二个 for 循环内。 In the place you have it now, you should have an empty println , to print a line break在你现在拥有的地方,你应该有一个空的println ,以打印换行符

  • Your i++ counter is wrong.你的i++计数器是错误的。 The first line of the chessboard ends with white and the second continues with white.棋盘的第一行以白色结束,第二行以白色继续。 With i++ you lose that.有了i++你就失去了它。 You could remove i altogether.你可以完全删除i A sufficient condition is: if (r + c) % 2 == 0 it's white, else black.一个充分条件是:if (r + c) % 2 == 0它是白色的,否则是黑色的。

  1. Use char type instead of int to represent the matrix使用char类型代替int来表示矩阵
  2. Brackets on the second for are needed需要第二个 for 上的括号
  3. You need to add a break line to jump between rows (System.out.println()).您需要添加一个分隔线以在行之间跳转(System.out.println())。 Also add space " " on the first print if you want B and W be separated如果您希望 B 和 W 分开,还要在第一张打印件上添加空格“”
  4. (Style) You can initialise variables r and c on the for scope (样式)您可以在 for 范围内初始化变量 r 和 c

     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.您需要一个char数组而不是一个int数组。 (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.当它应该运行到 8 时,您的计数只运行到 7。使用board.lengthboard[r].length几乎board.length考虑这一点,这是最佳实践。

  3. You only need 2 variables (rows and columns), so I'd recommend removing misleading and superfluous variables whenever possible.您只需要 2 个变量(行和列),因此我建议尽可能删除误导性和多余的变量。

  4. Your for needs brackets to execute more than 1 line of code in a block.您的for需要括号才能在一个块中执行 1 行以上的代码。 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 .这导致了一个条件测试: (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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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