【问题标题】:Error when calling method in Java在Java中调用方法时出错
【发布时间】:2013-03-02 00:47:39
【问题描述】:

我有一个包含 3 个方法的类,它基本上用 Java 中的数组数组做一些基本的事情,尽管当我试图在我的 main 中调用这些方法时我得到一个错误。谁能告诉我问题是什么...我确定这是一些愚蠢的基本错误:(

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)

【问题讨论】:

  • 你遇到了什么错误?

标签: java arrays multidimensional-array


【解决方案1】:

线程“主”java.lang.Error 中的异常:未解决的编译 问题: m 无法解析为变量 在 testMatrix.main(testMatrix.java:5)

通过查看您的错误非常明显,您需要一个 Matrix 类的实例来访问其实例变量

 Matrix.negate(new Matrix().m);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多