2016-11-17 75 views
-4

我只是一个新手,当谈到编程和学生。我的任务是创建一个乘法表的二维数组,但我似乎得到相同的错误超时: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); 


      } 
     } 


    } 

} 

干杯,

谢谢!

+5

如何C++与你的问题? –

+0

提示:所有声明* ROWS和COLUMNS的* local *变量的循环都很糟糕,IMO。 'COLUMNS

+0

您是否尝试过调试以查看发生了什么? – n247s

回答

0

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

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(); 

     } 
    } 
0

第一重要的事情,你的循环变量都躲在类静态变量。这是一个糟糕的代码。 ROWS也是一个for循环计数器和一个静态类成员。该代码将正常工作,但您会对您尝试引用的内容感到困惑。

在打印方法

现在,有一个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-出声明,你指的ROWS(资本),你需要使用row即外部循环变量。

还要更改内部的for-loop变量名称。条件COLUMNS < COLUMNS是完全混淆;

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