如何打印二维数组

时间:2015-10-16 03:51:41

标签: java arrays

我创建了一个布尔2维数组,并尝试通过调用另一个方法将“A”(Config.ALIVE)打印为true,将“。”(Config.DEAD)打印为false。但我的代码只打印出A.可以有人告诉我为什么吗? 这是我的代码

public static void initializeGlider(boolean[][] world) {
        for (int row = 0; row < Config.WORLD_ROWS; row++) {
            for (int column = 0; column < Config.WORLD_COLUMNS; column++) {
                if (row==1&&column==1)
                    world[row][column] = true;
                 if (row==2&&column==2)
                     world[row][column] = true;
                if (row==2&&column==3)
                     world[row][column] = true;
                if (row==3&&column==1)
                     world[row][column] = true;
                 if (row==3&&column==2)
                    world[row][column] = true;
                else
                    world[row][column] = false;
            }
        }

        printWorld("Glider",world,0);
    }
        public static void printWorld(String patternName, boolean[][] world,
            int generationNum) {
        int row = 0;
        int column = 0;
        for (row = 0; row < Config.WORLD_ROWS; row++) {
            // for each column in the world
            for (column = 0; column < Config.WORLD_COLUMNS; column++) {
                // if the cell is alive
                if (world[row][column] = true)
                    System.out.print(Config.ALIVE);
                // otherwise if the cell is dead.
                if (world[row][column] = false)
                    System.out.print(Config.DEAD);


                // print out the number of cells alive.
            }
            System.out.println();
        }
    }

1 个答案:

答案 0 :(得分:0)

请在此处查看您的代码

 if (world[row][column] = true)
     System.out.print(Config.ALIVE);
 // otherwise if the cell is dead.
 if (world[row][column] = false)
    System.out.print(Config.DEAD);

我认为会是

if (world[row][column] == true)
      System.out.print(Config.ALIVE);
 // otherwise if the cell is dead.
 if (world[row][column] == false)
      System.out.print(Config.DEAD);