2013-03-02 152 views
-4

我有一個包含3個方法的類,它基本上用Java中的數組數組實現了一些基本的東西,但是當試圖在我的主函數中調用這些方法時,我得到一個錯誤..任何人都可以告訴我什麼問題是...我敢肯定它的一些愚蠢的錯誤基本:(在Java中調用方法時出錯

class Matrix { 
    double[][] m = { {2,4,31,31}, 
        {3,3,21,41}, 
        {1,2,10,20}, 
        {3,2,20,30} }; 

    public static void negate(double[][] m){ 
     int r = m.length; 
     int c = m[r].length; 
     double[][] n = new double[c][r]; 
     for(int i = 0; i < n.length; ++i) { 
      for(int j = 0; j < n[i].length; ++j) { 
       n[i][j] = (m[i][j])*-1; 
      } 
     } 

    } 

    public static void transposeMatrix(double[][] m){ 
     int r = m.length; 
     int c = m[r].length; 
     double[][] t = new double[c][r]; 
     for(int i = 0; i < r; ++i){ 
      for(int j = 0; j < c; ++j){ 
       t[j][i] = m[i][j]; 
      } 
     } 

    } 

    public void print(double[][] n, double[][] t){ 
     int r = m.length; 
     int c = m[r].length; 

     for(int i = 0; i < r; ++i){ 
      for(int j = 0; j < c; ++j){ 
      System.out.print(" " + n[i][j]); 
      } 
      System.out.println(""); 
      } 

     for(int i = 0; i < r; ++i){ 
      for(int j = 0; j < c; ++j){ 
      System.out.print(" " + t[i][j]); 
      } 
      System.out.println(""); 
      } 
    } 
} 

現在這是主要的,我有..提前任何輸入

public class testMatrix { 
    public static void main(String[] args){ 

     Matrix.negate(m); 
    } 

} 

感謝!

這是個錯誤...

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    m cannot be resolved to a variable 

    at testMatrix.main(testMatrix.java:5) 
+3

你得到什麼錯誤實例變量? – mindandmedia 2013-03-02 00:48:43

+0

重建所有.... – jdb 2013-03-02 00:52:29

回答

4

Exception in thread "main" java.lang.Error: Unresolved compilation problem: m cannot be resolved to a variable at testMatrix.main(testMatrix.java:5)

通過看你的錯誤了很明顯的,你需要的Matrix類的一個實例來訪問其

Matrix.negate(new Matrix().m); 
相關問題