当前位置: 首页 > 知识库问答 >
问题:

错误-''在类节点中找不到main方法,请在下面的代码中将main方法定义为....“:

韩鸿
2023-03-14

确切错误:“错误:在类节点中找不到Main方法,请将Main方法定义为:public static void Main(string[]args)或JavaFX应用程序类必须扩展JavaFX.application.application”

class Node{
    int key;
    Node left, right;

    public Node(int item){
        key = item;
        left = right = null;
    }
}

class BinaryTree{
    Node root;

    BinaryTree(){
        root = null;
    }

    void printPostorder(Node node){
        if(node == null)
            return;
        
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.key + " ");
    }

    void printPostorder(){ printPostorder(root);}

    public static void main(String[] args){
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);

        System.out.println("\nPostorder: ");
        tree.printPostorder();
    }
}

但是主要功能已经定义了。

共有1个答案

韦智刚
2023-03-14

我怀疑您将java文件命名为“node.java”而不是“binarytree.java”,代码会引发错误,因为在类node中没有任何主函数(您正在尝试运行),而是在类binarytree中没有主函数。如果将文件重命名为BinaryTree,该问题将得到解决。

 类似资料: