2016-11-17 109 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); 
    } 
}