Java超出了错误

时间:2015-01-10 01:28:54

标签: java arrays

目前我正在撰写“生命游戏”计划。然而,当我尝试打印出随机数组时,没有任何反应,当我尝试运行预构建的网格时,我得到一个越界错误。

请记住,我已经尝试过调试自己并让其他同伴看过这个问题,他也无法弄明白。

任何帮助将不胜感激。 (我为缺乏评论而道歉。我意识到这是多么不专业,但目前我只是想让它发挥作用。)

这是我的主要文件

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 个答案:

答案 0 :(得分:2)

初始化lifeArray时,

k和l为0。

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