简体   繁体   English

Java二维数组布尔值

[英]Java Two Dimensional Array Boolean

My homework is to create a (not so) simple recreation of Conway's Game of Life. 我的功课是创造(并非如此)康威生活游戏的简单娱乐方式。 Exact Instructions: 确切说明:

  1. Prompt the user to enter a list of (i,j) pairs (both non-negative integers) (stop when a negative integer is read for either i or j) 提示用户输入(i,j)对(两个非负整数)对的列表(当为i或j读取负整数时停止)
  2. Prompt the user to enter the number of time steps 提示用户输入时间步数
  3. Initialize the grid based on the (i,j) pairs entered by the user 根据用户输入的(i,j)对初始化网格
  4. Display the initial state of the grid (call the displayGrid() method) 显示网格的初始状态(调用displayGrid()方法)
  5. For each time step, update the grid according to Conway's rules (call the updateGrid() method) and display the grid (call the displayGrid() method) 对于每个时间步,根据Conway的规则更updateGrid()格(调用updateGrid()方法)并显示网格(调用displayGrid()方法)

My programs output has now changed: 我的程序输出现已更改:

Please enter list of (i, j) pairs for populated cells(negative i or j to quit):
6 4 6 5 6 6 6 7 6 8
-1 -1
Enter number of time steps:
2
Initial Grid:






   ######



Time step 1










Time step 2

My new question is what is making my code "kill" everything? 我的新问题是什么使我的代码“杀死”一切? Why isn't there any "true"s? 为什么没有“真”?

Here's my code: 这是我的代码:

java.util.Scanner;

  class P8{
    public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       boolean[][] multi = new boolean[10][10];
       int i, j, N, x, y, h;

       for(x=1; x<multi.length; x++){
          for(y=1; y<multi.length; y++){
             multi[x][y] = false;
          }
       }
       System.out.println("Please enter list of (i, j) pairs for populated cells(negative i or j to quit):");

       while(true){
          i = in.nextInt();
          j = in.nextInt();
          if(i <= 0 || j <= 0){
             break;}

          multi[i][j] = true;
          }

       System.out.println("Enter number of time steps:");
       N = in.nextInt();
       System.out.println("Initial Grid: \n");
       displayGrid(multi);
       for(i = 1; i<N+1; i++){
          System.out.printf("Time step %d\n", i);
          updateGrid(multi);
          displayGrid(multi);
          System.out.println("\n");
       }
 }

       /******************************************
       void updateGrid(boolean mat[][]); updates
       the 2 dimensional array using Conway's
       standard rules. Does this by calling
       "neighbors" function
       ******************************************/
       public static void updateGrid(boolean mat[][]){
          boolean[][] temp = new boolean[mat.length][mat.length];
          int i, j, row, col, n=0;

          for(row = 0; row < mat.length; row++){
             for(col = 0; col < mat.length; col++){
             neighbors(mat, row, col);

             if(n>=4 || n<=1)
                mat[row][col] = false;
             else
                mat[row][col] = true;

             }
          }
       }

       /******************************************
        void neighbors(boolean mat[][]int row, int col) 
        checks how many "neighbors" are around a given point
       ******************************************/
        public static int neighbors(boolean mat[][], int row, int col){
           int N =0;
           if((row-1 >= 0)&&(col-1 >= 0))
              N = mat[row-1][col-1] ? N + 1 : N;

           if((row >=0)&&(col-1 >= 0))
              N = mat[row][col-1] ? N+1 : N;

           if((row+1 < mat.length)&&(col-1 >= 0))
              N = mat[row+1][col-1] ? N+1 : N;

           if((row+1 < mat.length)&&(col < mat[0].length))
              N = mat[row+1][col] ? N+1 : N;

           if((row+1 < mat.length)&&(col+1 < mat[0].length))
              N = mat[row+1][col+1] ? N+1 : N;

           if((row < mat.length)&&(col+1 < mat[0].length))
              N = mat[row][col+1] ? N+1 : N;

           if((row-1 >= 0)&&(col+1 < mat[0].length))
              N = mat[row-1][col+1] ? N+1 : N;

           if((row-1 >= 0)&&(col < mat[0].length))
              N = mat[row-1][col] ? N+1 : N;

            return N;
       }

       /******************************************
        void displayGrid(int mat[][]) prints out
        a two dimensional array
       ******************************************/
       public static void displayGrid(boolean mat[][]){
          int x, y;
          for(x=1; x<mat.length; x++){
             for(y = 1; y<mat.length; y++){
                if(mat[x][y])
                   System.out.printf("#");
                else
                   System.out.printf(" ");
             }
             System.out.println();
          }

       }
  }

` `

My question is where in my code is multi[][] becoming all 'true' since that's the criteria I have to print '#' 我的问题是我的代码中multi [] []在哪里变成“真”,因为这是我必须打印“#”的标准

In your displayGrid method. 在您的displayGrid方法中。

if(mat[x][y]= true) is an assignment, not a comparison. if(mat[x][y]= true)是一个赋值,而不是一个比较。

Use 采用

if (mat[x][y])

instead. 代替。

You never need to write bool == true (or bool == false ), because you can write them more easily as bool (and !bool ) - and the latter forms avoid the potential for missing out one of the = signs and changing the semantics of the expression. 您永远不需要写bool == true (或bool == false ),因为您可以更轻松地将它们写为bool (和!bool )-后一种形式可以避免漏掉=号之一并更改。表达式的语义。

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

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