2017-05-31 67 views
-2

我們試圖完成我們的類項目。但是當我們試圖運行下面的java代碼時,有4個我們無法弄清楚的錯誤。這些是錯誤。代碼中的非法表達式(Java)

Bst.java:137: error: illegal start of expression 
            for() 
             ^
Bst.java:139: error: illegal start of expression 
            } 
            ^
Bst.java:140: error: 'else' without 'if' 
            else{ 
            ^
Bst.java:164: error: reached end of file while parsing 

這裏是我們的Java代碼2016春季數據結構BST示例代碼在MIU(對於項目#2)。我們可以添加/修改類,但應該保持類的基本結構。

import java.util.*; 
import java.util.*; 
class Node { 
    int data; // data 
    Node lc; //left child pointer 
    Node rc; //right child pointer 
    Node(int data){  // constructor 
     this.data=data; 
     lc=null; 
     rc=null; 
    } 
} 
class BST { 
    Node root; 
    BST(){ 
     root=null; 
    } 
    void insert(int newData){ // This is not the full version 
     Node newNode=new Node(newData); 
     if(root==null) root=newNode; // root was null 
     else { // root was not null, so find the place to attach the new node 
      Node ptr=root; // Node pointer 
      while(true){ 
       if(newNode.data==ptr.data) { 
        System.out.println("The node already exist."); 
        break; 
       } 
       else if(newNode.data<ptr.data) { 
        if(ptr.lc!=null) ptr=ptr.lc; 
        else { 
         ptr.lc=newNode; 
         System.out.println(newNode.data+" was successfully inserted."); 
         break; 
        } 
       } 
       else if(newNode.data>ptr.data) { 
        if(ptr.rc!=null) ptr=ptr.rc; 
        else { 
         ptr.rc=newNode; 
         System.out.println(newNode.data+" was successfully inserted."); 
         break; 
        } 
       } 
      } 
     }    
    } 
    Node delete(Node root,int key){  // delete the nodes 
      if (root == null) return null; 
      if (root.data > key) { 
       root.lc = delete(root.lc, key); 
       } 
       else if (root.data < key) { 
      root.rc = delete(root.rc, key); 
     } 
     return null; 
    } 
    boolean search(Node root,int key){ // if the search is successful return the Node or return null 

     if(root==null){ 
      return false; 
     } 
     if(root.data== key) { 
      return true; 
     } 
      if(root.data!= key){ 
       return false; 
      } 
     return true; 
    } 
    int number_nodes(Node n){ // the counting left child 
     if(n == null) 
     return 0; 
     if(n.lc ==null && n.rc==null) 
     return 1; 
     else 
      return 1+number_nodes(n.lc); 
    } 
    int rnumber_nodes(Node n){ // the counting right child 
     if(n == null) 
     return 0; 
     if(n.lc ==null && n.rc==null) 
     return 1; 
     else 
     return 1+number_nodes(n.rc); 
    } 
    void inorder(Node n){ // recursive inorder travelsal 
     if(n==null) return; 
     System.out.print("["); 
     inorder(n.lc); 
     System.out.print(n.data); 
     inorder(n.rc); 
     System.out.print("]"); 
    } 
    void preorder(Node n){ // recursive preorder travelsal 
     if(n==null) return; 
     System.out.print("["); 
     System.out.print(n.data); 
     preorder(n.lc);   
     preorder(n.rc); 
     System.out.print("]"); 
    } 
    void postorder(Node n){ // recursive postorder travelsal 
     if(n==null) return; 
     System.out.print("[");  
     postorder(n.lc);   
     postorder(n.rc); 
     System.out.print(n.data); 
     System.out.print("]"); 
    } 
} 
public class Bst { // change the name into your IDs 
    public static void main(String[] args) { 
     BST bst=new BST(); 
     Scanner sScan=new Scanner(System.in); // menu 
     Scanner iScan=new Scanner(System.in); // data 
     while(true){ 
      System.out.print("\n(q)uit,(i)nsert,(d)elete,(s)earch,i(n)order,(p)reorder,p(o)storder,(h)ow many:"); 
      String uChoice=sScan.next(); 
      if(uChoice.equalsIgnoreCase("i")){ 
       System.out.print("Enter a number to insert:"); 
       int uData=iScan.nextInt(); 
       bst.insert(uData); 
      } 
      else if(uChoice.equalsIgnoreCase("d")){ // deletion 
       System.out.print("enter the delete number"); 
       Scanner s=new Scanner(System.in); // to use new scanner 
       int delete_num=s.nextInt(); // 
       bst.delete(bst.root,delete_num); 
      } 
      else if(uChoice.equalsIgnoreCase("s")){ // search 
       System.out.print("enter the search number"); 
       Scanner s=new Scanner(System.in); 
       int search_num=s.nextInt(); 
        if(bst.search(bst.root,search_num)){ 
         for(){ 
         System.out.println(" your number is found"); // to tell you your # is found or not found 
        } 
        } 
        else{ 
         System.out.println(" your number is not found"); 
        } 
      } 
      else if(uChoice.equalsIgnoreCase("n")){ // in order 
       bst.inorder(bst.root); 
      } 
      else if(uChoice.equalsIgnoreCase("p")){ // pre order 
       bst.preorder(bst.root); 
      } 
      else if(uChoice.equalsIgnoreCase("o")){ // post order 
       bst.postorder(bst.root); 
      } 
      else if(uChoice.equalsIgnoreCase("h")){ // how many 
       int x,y; 
       x=bst.number_nodes(bst.root); 
       y=bst.rnumber_nodes(bst.root); 
       int total=x+y; 
       System.out.print(total); 
      } 
      if(uChoice.equalsIgnoreCase("q")) break; // quit 
     } 
    } 
} 
+0

只要刪除'for(){'loop並且所有東西都會合並 –

回答

1

你必須學習for循環的語法。在java中無限for循環寫如下:

for(;;){ } 

我建議你在這種情況下使用while循環。下面是一個例子,這兩個案例:

if(bst.search(bst.root,search_num)){ 
    for(;;){ 
      System.out.println(" your number is found"); 
    } 
} 
else{ 
    System.out.println(" your number is not found"); 

}

使用while循環:

if(bst.search(bst.root,search_num)){ 
     while(true){ 
       System.out.println(" your number is found"); 
     } 
    } 
    else{ 
     System.out.println(" your number is not found"); 
    } 

逸岸你的情況爲循環或同時不需要循環可言,因此代碼會如下所示::

if(bst.search(bst.root,search_num)){ 
    System.out.println(" your number is found"); 
} 
else{ 
    System.out.println(" your number is not found"); 
} 
0

在java中無限的循環寫爲:

for(;;){ } 

,而不是for(){ }

所以實際上你缺少分號正在for循環聲明無效/不完整,這就是錯誤的原因!