需要帮助代码无效

时间:2016-11-17 12:41:18

标签: java

在编程和学生方面,我只是个新手。我的任务是创建一个乘法表的二维数组,但我似乎得到了同样的错误:java.lang.arrayindexoutofboundsexception 10

请帮助。

下面是代码:

 public class MulTtable {

// rows and columns are declared as constants
 static final int ROWS= 10;
 static final int COLUMNS= 10;

// prints the content of the 2-D array
public static void printTable(char mt[][]){
int n=ROWS;


    for (int row = 0; row < ROWS; row++){

        for (int COLUMNS = 0; COLUMNS < COLUMNS; COLUMNS++){
        {
            System.out.print(mt[ROWS][COLUMNS] + "\t");
        }
        System.out.println();

    }
}
}

public static void main(String[] args){

    int mTable[][]= new int[ROWS][COLUMNS];

    for ( int ROWS = 0; ROWS < mTable.length; ROWS++){ 

        for ( int COLUMNS = 0; ROWS < mTable[ROWS].length; COLUMNS++){
            if (ROWS<11) // stores integer 1+1 in the first row of the array
              mTable[ROWS][COLUMNS] = 1+1;
              else
              mTable[ROWS][COLUMNS] = (ROWS)* (COLUMNS);


            }
        }


    }

}

干杯,

谢谢!

3 个答案:

答案 0 :(得分:0)

它不打印任何东西,因为你的打印表方法中有一个{太muc而且你的命名必须改变......工作代码:

static final int ROWS = 10;
    static final int COLUMNS = 10;

    public static void main(String[] args) {

        int mTable[][] = new int[ROWS][COLUMNS];

        for (int ROWS = 0; ROWS < mTable.length; ROWS++) {

            for (int COLUMNS = 0; COLUMNS < mTable[ROWS].length; COLUMNS++) {
                if (ROWS < 11)
                    mTable[ROWS][COLUMNS] = 1 + 1;
                else
                    mTable[ROWS][COLUMNS] = (ROWS) * (COLUMNS);

            }
        }
        printTable(mTable);
    }

    public static void printTable(int[][] mTable) {

        for (int row = 0; row < ROWS; row++) {

            for (int col = 0; col < COLUMNS; col++) {
                System.out.print(mTable[row][col] + "\t");
            }
            System.out.println();

        }
    }

答案 1 :(得分:0)

首先,你的循环变量隐藏了类静态变量。这是一个糟糕的代码。 ROWS也是一个for循环计数器和一个静态类成员。代码可以正常工作,但是你会对你想要引用的东西感到困惑。

现在在print方法中,有一个arrayIndexOutOfBound,即你正在访问数组边界之外的元素

 for (int row = 0; row < ROWS; row++){
    for (int COLUMNS = 0; COLUMNS < COLUMNS; COLUMNS++){
        System.out.print(mt[ROWS][COLUMNS] + "\t");
    System.out.println();

在最里面的sys-out语句中,您正在引用ROWS(大写),您需要使用row,即外部循环变量。

还要更改内部for循环变量名称。条件COLUMNS&lt; COLUMNS完全令人困惑;

答案 2 :(得分:0)

这是打印一个propper乘法表的那个:

public class MulTtable 
{
    static final int ROWS = 15;
    static final int COLUMNS = 15;

    public static void printTable(int mt[][])
    {
        for(int r = 0; r < mt.length; r++)
        {
            for(int c = 0; c < mt[r].length; c++)
            {
                {
                    System.out.print(mt[r][c] + "\t");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args)
    {
        int mTable[][] = new int[ROWS][COLUMNS];
        for(int r = 0; r < ROWS; r++)
        {
            for(int c = 0; c < COLUMNS; c++)
            {
                if(r < 1)
                {
                    mTable[r][c] = c;
                }
                else if(c < 1)
                {
                    mTable[r][c] = r;
                }
                else
                {
                    mTable[r][c] = r * c;
                }
            }
        }
        printTable(mTable);
    }
}
相关问题