2016-11-22 154 views
-1

我的作業是創建一個(不那麼簡單)康威的生命遊戲娛樂。確切說明:Java二維陣列布爾

  1. 提示用戶輸入的(I,J)對(兩個非負整數)的列表(當負整數讀取停止或者i 或j)
  2. 提示用戶輸入的時間的步數
  3. 初始化基於第(i,j),通過用戶
  4. 顯示網格的初始狀態進入雙網格(調用displayGrid()方法)
  5. 對於每個時間步驟,更新網格acc奧爾丁康威的規則(也稱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(); 
      } 

     } 
    } 

`

+0

「else if」沒有意義。你可以使用'else',因爲你只是反轉原始條件。 – shmosel

+0

我考慮了您的意見並更新了我的代碼。 :D儘管出現了一個新問題,我相信它存在於我的neighbours()函數中,但我似乎無法找到它。 – 54mike

回答

2

我的問題就是在我的代碼是多[] []成爲所有「真」,因爲這是我必須打印的標準「# '

在你的displayGrid方法中。

if(mat[x][y]= true)是一項任務,而不是比較。

使用

if (mat[x][y]) 

代替。

你永遠需要編寫bool == true(或bool == false),因爲你可以將它們更容易寫成bool(和!bool) - 後者的形式避免了潛在的錯過了的=標誌之一,改變的語義表達。

+0

啊,好吧,我可以看到你在看什麼。我會改進我的代碼。 – 54mike