编译错误缺少东西

compile error something missing

这个简单的问题我几乎不需要帮助。我收到编译器错误,但是 我不知道如何消除这个错误。 此行显示错误 int n = totalTree(num);
这是我的代码:

public class TotalNumberOfBinaryTrees {

    //static int elementCount = 50;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int test =sc.nextInt();
        while(test>0){
            int num = sc.nextInt();
            int n = totalTree(num);
            System.out.println("totalTree"+n);
            test--;
        }
    }

    public int totalTree(int n) {
    if (n == 1 || n == 0)
            return 1;
    else {
            int left = 0;
            int right = 0;
            int sum = 0;
            for (int k = 1; k <= n; k++) {
        left = totalTree(k - 1);
        right = totalTree(n - k);
        sum = sum + (left * right);
            }
    return sum;
        }
    }
}

totalTree 是一个非静态方法。如果不创建 class.

的实例,则不能从静态方法 (main) 调用它

我不确定它是否有意义,但你可以调用它:

int n = new TotalNumberOfBinaryTrees().totalTree(num);

或者改成静态方法

您不能从静态方法访问非静态方法,因此将方法声明为:

public static int totalTree(int n) {}  

现在下面的代码将没有编译问题:

public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int test =sc.nextInt();
        while(test>0){
            int num = sc.nextInt();
            int n = totalTree(num);
            System.out.println("totalTree"+n);
            test--;
        }
    }

    public static int totalTree(int n) {
        if (n == 1 || n == 0)
            return 1;
        else {
            int left = 0;
            int right = 0;
            int sum = 0;
            for (int k = 1; k <= n; k++) {
                left = totalTree(k - 1);
                right = totalTree(n - k);
                sum = sum + (left * right);
            }
            return sum;
        }
    }