2015-01-10 53 views
-1

當前我正在編寫生命遊戲程序。但是,當我嘗試打印出隨機數組時,沒有任何反應,並且當我嘗試運行預先構建的網格時,出現越界錯誤。Java outofbounds錯誤

請記住,我試圖調試自己,並有這個由另一個同行看過去,他也無法弄清楚。

任何幫助,將不勝感激。 (我的不足評論道歉。我知道如何不專業,這是的,但我目前只是想獲得它的工作。)

這裏是我的主文件

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.*; 

public class Life 
{ 
    private int k; 
    private int l; 
    private boolean[][] lifeArray = new boolean[k][l]; 
    private int counter = 0; 

    public Life(int thisK, int thisL) 
    { 
     lifeArray = new boolean[k][l]; 
     k = thisK; 
     l = thisL; 
    } 

    public void initialGrid() 
    { 
     int r = lifeArray.length/3; // Set up a third of the way down 
     int c = lifeArray[0].length/3; // Set up a third of the way over 
     for (int a = 0; a < lifeArray.length; a++) 
      for (int b = 0; b < lifeArray[0].length; b++) 
       lifeArray[a][b] = false; // initialize the array to false 

     lifeArray[r][c + 3] = lifeArray[r][c + 8] = true; // top row 
     lifeArray[r + 1][c + 1] = lifeArray[r + 1][c + 2] = true; // middle 2 
                    // le.t 
     lifeArray[r + 1][c + 9] = lifeArray[r + 1][c + 10] = true; // middle 4 
     lifeArray[r + 1][c + 4] = lifeArray[r + 1][c + 5] = true; // middle 4 
     lifeArray[r + 1][c + 6] = lifeArray[r + 1][c + 7] = true; // middle 2 
                    // right 
     lifeArray[r + 2][c + 3] = lifeArray[r + 2][c + 8] = true; // bottom row 
    } 

    public void randomBuild() { 
     Random rn = new Random(); 
     int counter = 0; 

     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       if (rn.nextInt(10) <= 3) { 
        lifeArray[i][j] = true; 
        System.out.println("*"); 
       } else { 
        System.out.println(" "); 
       } 
       counter++; 
      } 
      System.out.println("\n"); 
     } 
     printBoard(); 
    } 

    public void fileInput(String theFile, String directory) throws IOException { 
     File file = new File(theFile, directory); 
     Scanner scanIt = new Scanner(file); 

    } 

    public void prebuiltGrid() { 
     //clearGrid(); 
     initialGrid(); 
     printBoard(); 

    } 

    public void clearGrid() { 
     // Initializes array to all false 
     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       lifeArray[i][j] = false; 
      } 
     } 
    } 

    public void nextGen() { 
     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       if (lifeArray[i + 1][j] == true) { 
        counter++; 
       } else if (lifeArray[i - 1][j] == true) { 
        counter++; 
       } else if (lifeArray[i][j + 1] == true) { 
        counter++; 
       } else if (lifeArray[i][j - 1] == true) { 
        counter++; 
       } else if (lifeArray[i + 1][j + 1] == true) { 
        counter++; 
       } else if (lifeArray[i - 1][j - 1] == true) { 
        counter++; 
       } 

       if (counter >= 4 || counter <= 1) { 
        lifeArray[i][j] = false; 
       } else if (counter == 3) { 
        lifeArray[i][j] = true; 
       } 

       counter = 0; 
      } 
     } 
    } 

    public void printBoard() 
    { 
     int lineCount = 0; 
     for(int i = 0; i < lifeArray.length; i++) 
     { 
      for(int j = 0; j < lifeArray[0].length; j++) 
      { 
       if(lifeArray[i][j] == true) 
       { 
        System.out.println("*"); 
       } 
       else 
       { 
        System.out.println(" "); 
       } 
       lineCount++; 


      } 
      System.out.println("\n"); 
     } 
    } 
} 

這裏是我的跑步者

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.*; 

public class Life 
{ 
    private int k; 
    private int l; 
    private boolean[][] lifeArray = new boolean[k][l]; 
    private int counter = 0; 

    public Life(int thisK, int thisL) 
    { 
     lifeArray = new boolean[k][l]; 
     k = thisK; 
     l = thisL; 
    } 

    public void initialGrid() 
    { 
     int r = lifeArray.length/3; // Set up a third of the way down 
     int c = lifeArray[0].length/3; // Set up a third of the way over 
     for (int a = 0; a < lifeArray.length; a++) 
      for (int b = 0; b < lifeArray[0].length; b++) 
       lifeArray[a][b] = false; // initialize the array to false 

     lifeArray[r][c + 3] = lifeArray[r][c + 8] = true; // top row 
     lifeArray[r + 1][c + 1] = lifeArray[r + 1][c + 2] = true; // middle 2 
                    // le.t 
     lifeArray[r + 1][c + 9] = lifeArray[r + 1][c + 10] = true; // middle 4 
     lifeArray[r + 1][c + 4] = lifeArray[r + 1][c + 5] = true; // middle 4 
     lifeArray[r + 1][c + 6] = lifeArray[r + 1][c + 7] = true; // middle 2 
                    // right 
     lifeArray[r + 2][c + 3] = lifeArray[r + 2][c + 8] = true; // bottom row 
    } 

    public void randomBuild() { 
     Random rn = new Random(); 
     int counter = 0; 

     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       if (rn.nextInt(10) <= 3) { 
        lifeArray[i][j] = true; 
        System.out.println("*"); 
       } else { 
        System.out.println(" "); 
       } 
       counter++; 
      } 
      System.out.println("\n"); 
     } 
     printBoard(); 
    } 

    public void fileInput(String theFile, String directory) throws IOException { 
     File file = new File(theFile, directory); 
     Scanner scanIt = new Scanner(file); 

    } 

    public void prebuiltGrid() { 
     //clearGrid(); 
     initialGrid(); 
     printBoard(); 

    } 

    public void clearGrid() { 
     // Initializes array to all false 
     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       lifeArray[i][j] = false; 
      } 
     } 
    } 

    public void nextGen() { 
     for (int i = 0; i < lifeArray.length; i++) { 
      for (int j = 0; j < lifeArray[0].length; j++) { 
       if (lifeArray[i + 1][j] == true) { 
        counter++; 
       } else if (lifeArray[i - 1][j] == true) { 
        counter++; 
       } else if (lifeArray[i][j + 1] == true) { 
        counter++; 
       } else if (lifeArray[i][j - 1] == true) { 
        counter++; 
       } else if (lifeArray[i + 1][j + 1] == true) { 
        counter++; 
       } else if (lifeArray[i - 1][j - 1] == true) { 
        counter++; 
       } 

       if (counter >= 4 || counter <= 1) { 
        lifeArray[i][j] = false; 
       } else if (counter == 3) { 
        lifeArray[i][j] = true; 
       } 

       counter = 0; 
      } 
     } 
    } 

    public void printBoard() 
    { 
     int lineCount = 0; 
     for(int i = 0; i < lifeArray.length; i++) 
     { 
      for(int j = 0; j < lifeArray[0].length; j++) 
      { 
       if(lifeArray[i][j] == true) 
       { 
        System.out.println("*"); 
       } 
       else 
       { 
        System.out.println(" "); 
       } 
       lineCount++; 


      } 
      System.out.println("\n"); 
     } 
    } 
} 
+1

您能否在您的文章中包含錯誤,特別是堆棧跟蹤? – lared

回答

2

k和l在初始化lifeArray時爲0。

public Life(int thisK, int thisL) 
{ 
    k = thisK; 
    l = thisL; 
    lifeArray = new boolean[k][l]; 
}