二维循环OutOfBoundsException - Java

时间:2016-12-11 05:43:24

标签: java

我想知道你们是否可以帮我解决一些代码问题。我一直在编程这个拼图创建器,但是当我尝试为我的二维数组生成值时,我得到一个OutOfBoundsException。我确信这个解决方案很简单,但无论出于何种原因,我似乎无法找到错误的解决方案。 (当然,还有一个与此相关的驾驶员课程,但我不认为必须包括在这里。)

import java.util.*;

public class ThirtyWonderful 
{
    private int dimen;

    public ThirtyWonderful (int dimensions)
    {
        dimen = dimensions;
    }

    public ThirtyWonderful()
    {
        dimen = 5;
    }

    Random gen = new Random();
    int[][] nums = new int [dimen][dimen];

    public void genPuzzle()
    {
        for (int count = 0; count < dimen; count++)
        {   
            for(int col = 0; col < dimen; col++)
            {
                nums[count][col] = gen.nextInt(9) + 1;
            }
        }
        checkAcc();

        if(checkAcc() == true)
        {
            for (int count = 0; count < dimen; count++)
            {     
                for(int col = 0; col < dimen; col++)
                {
                    System.out.print(nums[count][col] + " ");
                }

                System.out.println();
            }
        }
    }  

    public boolean checkAcc()
    {
        int tot = 0;
        for (int count = 0; count < dimen; count++)
        {   
            for(int col = 0; col < dimen; col++)
            {
                tot += nums[count][col];
            }

            if(tot != 31)
                return false;
        }

        for (int count = 0; count < dimen; count++)
        {   
            for(int col = 0; col < dimen; col++)
            {
                tot += nums[count][col];
            }

            if(tot != 31)
                return false;
        }

        return true;
}

}

2 个答案:

答案 0 :(得分:0)

你用新的int [dimen] [dimen]声明nums;在dimen甚至初始化之前,默认维度值为0,所以nums用new int [0]初始化[0]

这是修复代码

public class ThirtyWonderful {

    private int dimen;

    Random gen = new Random();
    int[][] nums;

    public ThirtyWonderful(int dimensions) {
        this.dimen = dimensions;
        nums = new int[dimen][dimen];
    }

    public ThirtyWonderful() {
        this(5);
    }

    public void genPuzzle() {
        for (int count = 0; count < dimen; count++) {
            for (int col = 0; col < dimen; col++) {
                nums[count][col] = gen.nextInt(9) + 1;
            }
        }
        checkAcc();

        if (checkAcc() == true) {
            for (int count = 0; count < dimen; count++) {
                for (int col = 0; col < dimen; col++) {
                    System.out.print(nums[count][col] + " ");
                }

                System.out.println();
            }
        }
    }

    public boolean checkAcc() {
        int tot = 0;
        for (int count = 0; count < dimen; count++) {
            for (int col = 0; col < dimen; col++) {
                tot += nums[count][col];
            }

            if (tot != 31) {
                return false;
            }
        }

        for (int count = 0; count < dimen; count++) {
            for (int col = 0; col < dimen; col++) {
                tot += nums[count][col];
            }

            if (tot != 31) {
                return false;
            }
        }

        return true;
    }

}

答案 1 :(得分:0)

您应该在构造函数中初始化数组对象。您在实例化对象之前在类加载时初始化数组。