乘法表不工作数组索引超出范围

时间:2014-11-11 03:50:53

标签: java arrays arraylist multidimensional-array

我在这里添加了加1:for(int i = 1; i< x + 1; i ++){             for(int j = 1; j< y + 1; j ++) 所以程序会抓住所有数字,除了我之前所说的那个数字。 如果我为最大x值输入10,为y输入10(不加1),这就是输出显示的内容:

您想要功率表还是乘法表?  按1进行电源  按2进行乘法 2 最大x值? 10 最大y值? 10

1  2  3  4  5  6  7  8  9  
2  4  6  8  10  12  14  16  18  
3  6  9  12  15  18  21  24  27  
4  8  12  16  20  24  28  32  36  
5  10  15  20  25  30  35  40  45  
6  12  18  24  30  36  42  48  54  
7  14  21  28  35  42  49  56  63  
8  16  24  32  40  48  56  64  72  
9  18  27  36  45  54  63  72  81 

我不知道为什么这不起作用请帮助这是代码:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

    int defaulVa = 1;
    int defaulEx = 2;

    System.out.println("Would you like a power table or a multiplication table?"
            + "\n Press 1 for Power"
            + "\n Press 2 for Multiplicatiion");
    int a = sc.nextInt();

    System.out.println("Maximum x value?");
    int x = sc.nextInt();
    System.out.println("Maximum y value?");
    int y = sc.nextInt();

    switch (a) {
        case 1:
            System.out.println("How would you like your base?"
                    + "\nPress 1 for preset value"
                    + "\nPress 2 for acending value");
            int answer = sc.nextInt();
            switch (answer) {
                case 1:
                    System.out.println("What value?");
                    int value = sc.nextInt();
                    powerTable(x, y, value, defaulVa);
                    break;

                case 2:
                    System.out.println("How would you like your exponent?"
                            + "\nPress 1 for preset value"
                            + "\nPress 2 for acending value");
                    int d = sc.nextInt();

                    switch (d) {
                        case 1:
                            System.out.println("What value?");
                            int d2 = sc.nextInt();
                            powerTable(x, y, defaulVa, d2);
                            break;

                        case 2:
                            powerTable(x, y, defaulVa, defaulEx);
                            break;

                        default:

                            break;
                    }
                    break;

                default:

                    break;
            }

            break;
        case 2:
            multiplicationTable(x, y);
            break;
        default:
            System.out.println("I see you can't follow simple instructions...");
            System.exit(0);
            break;
    }

}

public static int[][] powerTable(int arrayDimensionX, int arrayDimensionY, int value, int value2) {
    int[][] myArray;
    myArray = new int[arrayDimensionX][arrayDimensionY];
    for (int i  = 1; i < myArray.length; i++) {
        for (int j = 1; j < myArray[i].length; j++) {
            if (value == 1) {
                value = myArray[i][j] = i * j;
            } else if (value != 1) {
                value++;
            }
            myArray[i][j] = ((int) Math.pow(value, value2));
        }
    }
    for (int i = 1; i < arrayDimensionX; i++) {
        for (int j = 1; j < arrayDimensionY; j++) {
            System.out.print(myArray[i][j] + "  ");
        }
        System.out.println();
    }
    return myArray;
}

public static int[][] multiplicationTable(int x, int y) {
    int[][] myArray;
    myArray = new int[x][y];
    for (int i = 1; i < myArray.length; i++) {
        for (int j = 1; j < myArray.length; j++) {
            myArray[i][j] = i * j;
        }
    }
    for (int i = 1; i < x + 1; i++) {
        for (int j = 1; j < y + 1; j++) {
            System.out.print(myArray[i][j] + "  ");
        }
        System.out.println();
    }
    return null;
}

}

2 个答案:

答案 0 :(得分:0)

数组已被0编入索引(它们从0开始,最后一个索引为length - 1

我会说...

for (int i = 1; i < x + 1; i++) {
    for (int j = 1; j < y + 1; j++) {
        System.out.print(myArray[i][j] + "  ");
    }
    System.out.println();
}

应该......

for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
        System.out.print(myArray[i][j] + "  ");
    }
    System.out.println();
}

您需要更改构建表格的循环,使其看起来更像......

for (int i = 0; i < myArray.length; i++) {
    for (int j = 0; j < myArray.length; j++) {
        myArray[i][j] = (1 + i) * (1 + j); // <-- Modify the indices here instead...
    }
}

有关详细信息,请查看Arrays

答案 1 :(得分:0)

为什么当您返回null时,multiplicationTable函数的返回类型为int[][]?如果你不打算让它返回阵列,那么最好让它成为一个无效函数。

无论如何,由于所有功能都在打印到屏幕上,因此功能过于复杂。可以做得更简单,像这样:

public static void multiplicationTable(int x, int y)
{
    for (int i = 1; i <= x; i++)
    {
        for (int j = 1; j <= y; j++)
            System.out.print(i * j);
        System.out.println();
    }
}

但是如果您希望它保持原样,那么您的ArrayIndexOutOfBounds例外很可能会发生:

for (int i = 1; i < x + 1; i++) {
    for (int j = 1; j < y + 1; j++) {
        System.out.print(myArray[i][j] + "  ");
    }
    System.out.println();
}

因为x和y是数组的大小,所以使循环转到i < x + 1i < y + 1将抛出异常,因为它试图访问不存在的索引。如果您将其更改为i < xi < y,则不应抛出异常:

for (int i = 1; i < x; i++) {
    for (int j = 1; j < y; j++) {
        System.out.print(myArray[i][j] + "  ");
    }
    System.out.println();
}

然而,即使这样也不会给你所需的输出,因为它会跳过数组的第一个元素,因为数组索引从0开始,而不是1,这意味着你的表中将缺少1的乘法。