2016-11-17 5 views
-4

Ich bin nur ein Anfänger, wenn es um die Programmierung geht, und ein Student. Ich wurde beauftragt, ein 2-D-Array einer Multiplikationstabelle zu erstellen, aber ich scheine, um den gleichen Fehler evertime zu bekommen: java.lang.arrayindexoutofboundexception 10Brauchen Sie Hilfe Code funktioniert nicht

Pls Hilfe.

Heres der Code:

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


      } 
     } 


    } 

} 

Cheers,

Me

Thank you!

+5

Wie wird C++ zu Ihrer Frage? –

+0

Hinweis: alle Ihre Schleifen, die * lokale * Variablen namens "ROWS" und "COLUMNS" deklarieren, sind ziemlich pleite, IMO. Die Schleifenbedingung von 'COLUMNS

+0

Haben Sie versucht, es zu debuggen, um zu sehen, was passiert? – n247s

Antwort

0

es wird nicht alles drucken, weil Sie ein { zu muc in Ihrem Drucktabellenmethode haben und Ihre Benennung muss sich ändern ... Arbeitscode:

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

erste wichtige Sache, Ihre Schleifenvariablen verstecken die Klasse -Static Variable. Dies ist ein schlechter Code. ROWS ist auch ein For-Schleife-Zähler und ein statisches Klassenmitglied. Der Code wird gut funktionieren, aber Sie werden verwirrt darüber, was Sie versuchen zu verweisen.

Jetzt im Druckverfahren gibt es ein arrayIndexOutOfBound dh Sie Element aus den Grenzen des Arrays

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

im innersten sys-out-Anweisung zugreifen, Sie sich beziehen auf ROWS (Kapital), Sie muss row verwenden, dh die äußere Schleifenvariable.

Ändern Sie auch den Variablennamen für die interne for-Schleife. die Bedingung COLUMNS < COLUMNS ist total verwirrend;

0

Hier ist einer, der eine Tabelle Propper Multiplikation druckt:

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