Java 越界错误



目前我正在编写生命游戏程序。但是,当我尝试打印出随机数组时,没有任何反应,当我尝试运行预构建的网格时,出现越界错误。

请记住,我试图调试自己,并被另一个同行查看过,他也无法弄清楚。

任何帮助将不胜感激。(对于缺乏评论,我深表歉意。我知道这是多么不专业,但目前我只是想让它工作。

这是我的主文件

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

k 和 l 在初始化 lifeArray 时为 0。

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

最新更新