簡體   English   中英

Java二維數組布爾值

[英]Java Two Dimensional Array Boolean

我的功課是創造(並非如此)康威生活游戲的簡單娛樂方式。 確切說明:

  1. 提示用戶輸入(i,j)對(兩個非負整數)對的列表(當為i或j讀取負整數時停止)
  2. 提示用戶輸入時間步數
  3. 根據用戶輸入的(i,j)對初始化網格
  4. 顯示網格的初始狀態(調用displayGrid()方法)
  5. 對於每個時間步,根據Conway的規則更updateGrid()格(調用updateGrid()方法)並顯示網格(調用displayGrid()方法)

我的程序輸出現已更改:

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

我的新問題是什么使我的代碼“殺死”一切? 為什么沒有“真”?

這是我的代碼:

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();
          }

       }
  }

`

我的問題是我的代碼中multi [] []在哪里變成“真”,因為這是我必須打印“#”的標准

在您的displayGrid方法中。

if(mat[x][y]= true)是一個賦值,而不是一個比較。

采用

if (mat[x][y])

代替。

您永遠不需要寫bool == true (或bool == false ),因為您可以更輕松地將它們寫為bool (和!bool )-后一種形式可以避免漏掉=號之一並更改。表達式的語義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM