2017-04-16 3 views
-1

Das Problem in calculateMainDiagonal Methode sein sollte, die Compiler-Fehler ich erhalte, ist:Kompilierungsfehler Schritte: int [] [] kann nicht Java in int umgewandelt werden

int[][] cannot be converted to int int sumD1 = calculateMainDiagonal(m);

Nun Haupt gleich bleiben sollte, wie es mein Professor ist Code. Jede Hilfe wird geschätzt. Ich glaube, ich werde einen int zurückgeben, aber ich bin mir nicht sicher. Ich

Versuche versucht:

public static int [][] calculateMainDiagonal(int sum);

public static int [][] calculateMainDiagonal(int [][] sum);

public static int calculateMainDiagonal(int sum)

Wenn ich dies einzugeben:

public static int calculateMainDiagonal(int [][] sum);

es gibt mir diese Compiler-Fehler statt:

java:50: error: missing method body, or declare abstract public static int calculateMainDiagonal(int [][] sum);

public class TesterProject 
{ 
    public static void main(String [] args) 
    { 
     int n = getMatrixSize(); 
     int[][] m = makeAndFillMatrix(n); 
     printMatrix(m); 

     int sumD1 = calculateMainDiagonal(m); 
     System.out.println("The sum of the main diagonal is " + sumD1); 
    } 
    public static int getMatrixSize() 
    { 
     Scanner S = new Scanner(System.in); 

     System.out.println("give me a int to create the matrix"); 
     int n = S.nextInt(); 
     return n; 
    } 
    public static int [][] makeAndFillMatrix(int n) 
    { 
     Random generator = new Random(5); 
     int [][] r = new int[n][n]; 
     int rand = generator.nextInt(10); 
     for(int i = 0; i < r.length; i++) 
     { 
      for(int j = 0; j < r[i].length; j++) 
      { 
       r[i][j]= rand; 
      } 
     } 
     return r; 
    } 
    public static void printMatrix(int [][] matrix) 
    { 
     for(int r = 0; r < matrix.length; r++) 
     { 
      for(int c = 0; c < matrix[r].length; c++) 
      { 
       System.out.print(matrix[r][c] + " "); 
      } 
      System.out.println(); 
     } 
    } 
    //only problem is this method 
    public static int calculateMainDiagonal(int sum); 
    { 
     int total = 0; 
     for (int r = 0; r < total; r++) 
     { 
      total += r; 
     } 
     return total; 
} 

}

+2

Die Linie, die in ihm 'calculateMainDiagonal' hat ein zusätzliches Semikolon erklärt. – ajb

+0

Rufen Sie auch nicht den Parameter 'sum' auf. Sie berechnen einen Wert aus einer Matrix. Die Matrix ist der Parameter, nicht die Summe. – ajb

+0

OMG, ich bin so dumm ... Ich habe stundenlang daran gearbeitet lol. Ich kann nicht glauben, dass ich das nicht bemerkt habe. – IDK

Antwort

1

Schlechtes Semikolon, und Ihr Parameter ist ein int[][] (kein int).

public static int calculateMainDiagonal(int[][] m) // <-- no semicolon. 
{ 
    int total = 0; 
    for (int r = 0; r < m.length; r++) // <-- not r < total. 
    { 
     if (r < m[r].length) { 
      total += m[r][r]; 
     } 
    } 
    return total; 
} 
0

Sie müssen das Semikolon am Ende des calculateMainDiagonal(int m) und ändern Sie den Parameter in calculateMainDiagonal(int[][] m) { ... } entfernen.

Sie müssen auch Ihre for Schleife in dieser Methode ändern, da sie den Parameter nicht berücksichtigt.

Etwas wie:

public static int calculateMainDiagonal(int[][] matrix) { 
    int total = 0; 
    for (int r = 0; r < matrix.length; r++) { 
     total += matrix[r][r]; 
    } 
    return total; 
} 
Verwandte Themen