eclipse上的语法错误[关闭]

Posted

技术标签:

【中文标题】eclipse上的语法错误[关闭]【英文标题】:Syntax error on eclipse [closed] 【发布时间】:2014-01-20 22:39:20 【问题描述】:

我的代码中的X's 变红了,因为我有关闭。他们对我来说都很好,IDE坚持认为我错了。我不确定为什么这是不正确的。有人可以指导我吗,谢谢!

错误发生在最后2个关闭

1 错误:

Syntax error on token "",  expected after this 
 token

2 错误:

Syntax error, insert "" to complete 
 ClassBody

这是我的代码

public class BinaryTree  
    // root node pointer. Will be null for an empty tree
    private Node root;

    /*
     -- Node --
     The binary tree is built using this nested node class.
     Each node stores on data element, and has left and right 
     sub-tree pointer which may be null.
     The node is a "dumb" nested class -- we just use it for storage; 
     */

    private static class Node      // Node class
        Node left;
        Node right;
        int data;

        Node(int newData)          // create Node
            left = null;
            right = null;
            data = newData;
        
    

    /*
     Creates an empty binary tree == null root pointer
     */

    public void BinaryTree() 
        root = null;
    

    /*
     Returns true if the given target is in the binary tree
     */

    public boolean lookup(int data)        // look up a number 
        return(lookup(root, data));         // use this to parse through a tree to search for element
    

    /*
     recursive lookup -- given a node, recur
     down searching for the given data.
     */
    private boolean lookup(Node node, int data) 
        if (node == null) 
            return(false);
        

        if (data == node.data) 
            return(true);
        
        else if (data < node.data) 
            return(lookup(node.left, data));
        
        else 
            return(lookup(node.right, data));
        
    

    public void insert(int data) 
        root = insert(root, data);
    

    /*
     Recursive insert -- given a pointer, recur down 
     and insert the given data into the tree. Returns the new 
     node pointer (the standard way to communicate 
     a changed pointer back to the caller).
     */

    private Node insert(Node node, int data) 
        if (node == null) 
            node = new Node(data);
        
        else 
            if (data <= node.data) 
                node.left = insert(node.left, data);
            
            else 
                node.right = insert(node.right, data);
            
        
        // I get an error here    #1

    return (node);
        // I also get an error here   #2

【问题讨论】:

return (node); 不在方法内。它可能应该高 2 行... 请不要再为这样一个微不足道的问题发布答案。我相信 OP 可以在一点点帮助下自己回答他的问题。 通常日食会获胜.. 【参考方案1】:

您在任何方法之外的类末尾返回,这是一个错误。

【讨论】:

【参考方案2】:

完整的工作代码

public class BinaryTree  
    // root node pointer. Will be null for an empty tree
    private Node root;

    /*
     -- Node --
     The binary tree is built using this nested node class.
     Each node stores on data element, and has left and right 
     sub-tree pointer which may be null.
     The node is a "dumb" nested class -- we just use it for storage; 
     */

    private static class Node      // Node class
        Node left;
        Node right;
        int data;

        Node(int newData)          // create Node
            left = null;
            right = null;
            data = newData;
        
    

    /*
     Creates an empty binary tree == null root pointer
     */

    public void BinaryTree() 
        root = null;
    

    /*
     Returns true if the given target is in the binary tree
     */

    public boolean lookup(int data)        // look up a number 
        return(lookup(root, data));         // use this to parse through a tree to search for element
    

    /*
     recursive lookup -- given a node, recur
     down searching for the given data.
     */
    private boolean lookup(Node node, int data) 
        if (node == null) 
            return(false);
        

        if (data == node.data) 
            return(true);
        
        else if (data < node.data) 
            return(lookup(node.left, data));
        
        else 
            return(lookup(node.right, data));
        
    

    public void insert(int data) 
        root = insert(root, data);
    

    /*
     Recursive insert -- given a pointer, recur down 
     and insert the given data into the tree. Returns the new 
     node pointer (the standard way to communicate 
     a changed pointer back to the caller).
     */

    private Node insert(Node node, int data) 
        if (node == null) 
            node = new Node(data);
        
        else 
            if (data <= node.data) 
                node.left = insert(node.left, data);
            
            else 
                node.right = insert(node.right, data);
            
        
        // I get an error here    #1

    return (node);
        // 

【讨论】:

【参考方案3】:

将插入更改为:

  private Node insert(Node node, int data) 
            if (node == null) 
                node = new Node(data);
            
            else 
                if (data <= node.data) 
                    node.left = insert(node.left, data);
                   
                else 
                    node.right = insert(node.right, data);
                     
                 
                 return (node);
            

在 eclipse 中:按 Ctrl + a 然后 Ctrl+shift+f 格式化代码..你会很容易知道这样的错误......

【讨论】:

【参考方案4】:

错误是因为你有以下语句

return (node);

在你的 insert 方法之外

【讨论】:

【参考方案5】:
    private Node insert(Node node, int data) 
            if (node == null) 
                node = new Node(data);
            
            else 
                if (data <= node.data) 
                    node.left = insert(node.left, data);
                
                else 
                    node.right = insert(node.right, data);
                
       -->      return (node);// I get an error here    #1
--->       

你在外面写了return语句。我把它搬了进去。请更新。

【讨论】:

以上是关于eclipse上的语法错误[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

WHERE选择上的MySQL语法错误[关闭]

普通eclipse 如何去掉语法检查?

解析错误:语法错误,意外的“公共”(T_PUBLIC)[关闭]

关闭CDT中自定义关键字的语法错误标记

eclipse标记import上有语法错误

关闭日食错误(这不是真正的错误)[重复]