繁体   English   中英

Java错误中非法的表达式开始

[英]illegal start of expression in Java Error

我对Java有点陌生,我的Java程序有一个我似乎无法修复的错误,非常简单的解决方案,我只是看不到它哈哈。 我怎样才能解决这个问题? 我尝试了几件事,但彼此之间又增加了更多错误。 谢谢你们!

 import java.io.*;
 import java.util.*;

 class Node {
      public char ch;
      public Node leftChild;
      public Node rightChild;
      Node(char c) {    
          ch = c;    
      }

      public void displayNode() {    
          System.out.print(ch);    
      }
 }

 class Tree {
      public Node root;
      public Tree(Node nd) {    
          root = nd;    
      }
      public void traverse(int traverseType) {
      switch (traverseType) {
          case 1:
              System.out.print(" \n Preorder traversal : ");
              preOrder(root);
            break;
          case 2:
              System.out.print(" \n Inorder traversal : ");
              inOrder(root);
            break;
          case 3:
              System.out.print(" \n Postorder traversal : ");
              postOrder(root);
            break;
       }
       System.out.println();
  }

  private void preOrder(Node localRoot) {
      if (localRoot != null) {
          localRoot.displayNode();
          preOrder(localRoot.leftChild);
          preOrder(localRoot.rightChild);
      }
  }

  private void inOrder(Node localRoot) {
      if (localRoot != null) {
          inOrder(localRoot.leftChild);
          localRoot.displayNode();
          inOrder(localRoot.rightChild);
      }
  }

  private void postOrder(Node localRoot) {
      if (localRoot != null) {
          postOrder(localRoot.leftChild);
          postOrder(localRoot.rightChild);
          localRoot.displayNode();
      }
  }

  public void displayTree() {
      Stack globalStack = new Stack();
      globalStack.push(root);
      int nBlanks = 32;
      boolean isRowEmpty = false;
      System.out.println(" ...................................................... ");
      while (isRowEmpty == false) {
          Stack localStack = new Stack();
          isRowEmpty = true;    
          for (int j = 0; j < nBlanks; j++)
              System.out.print(' ');    
              while (globalStack.isEmpty() == false) {
                  Node temp = (Node) globalStack.pop();
                  if (temp != null) {
                  temp.displayNode();
                  localStack.push(temp.leftChild);
                  localStack.push(temp.rightChild);

                  if (temp.leftChild != null || temp.rightChild != null)
                       isRowEmpty = false;
                  } else {
                       System.out.print("-");
                       localStack.push(null);
                       localStack.push(null);
             }
             for (int j = 0; j < nBlanks * 2 - 1; j++)
                  System.out.print(' ');
             }
             System.out.println();
             nBlanks / = 2;
             while (localStack.isEmpty() == false)
                  globalStack.push(localStack.pop());
             }
             System.out.println(" ...................................................... ");
         }
   }

 class BottomUp {
     private String inString;
     private int strlen;
     private Tree[] treeArray;
     private Tree aTree;
     private int numNodes;
     BottomUp(String s) {
         inString = s;
         strlen = inString.length();
         treeArray = new Tree[100];    
         for (int j = 0; j < strlen; j++) {
             char ch = inString.charAt(j);
             Node aNode = new Node(ch);
             treeArray[j] = new Tree(aNode); 
         }
    }

    public Tree getTree() {    
        return aTree;    
    }

    public void balanced() {
        numNodes = strlen;
        while (numNodes > 1) {
             int i = 0;
             int j = 0;
             Tree[] tempArray = new Tree[100];
             for (j = 0; j < strlen - 1; j++) {
                 Tree tree1 = treeArray[j];
                 Tree tree2 = treeArray[j + 1];    
                 Node aNode = new Node('+');
                 aTree = new Tree(aNode);
                 aTree.root.leftChild = tree1.root;
                 aTree.root.rightChild = tree2.root;
                 tempArray[i++] = aTree;
                 numNodes--;
                 j++;
              }
              if (strlen % 2 == 1) {
                 Tree tree1 = treeArray[j];
                 Node aNode = new Node('+');
                 aTree = new Tree(aNode);
                 aTree.root.leftChild = tree1.root;
                 tempArray[i++] = aTree;
              }
              treeArray = tempArray;
              strlen = numNodes;
         }
         aTree = treeArray[0];
     }
 }

 class BottomUpApp {
      public static void main(String[] args) throws IOException {
          BottomUp bup;
          Tree theTree = null;
          int value;
          String str;    
          while (true) {
               System.out.print(" Enter first letter of ");
               System.out.print(" balanced , show , or traverse : ");
               int choice = getChar();
               switch (choice) {
               case 'b':
                    System.out.print(" Enter string : ");
                    str = getString();
                    bup = new BottomUp(str);
                    bup.balanced();
                    theTree = bup.getTree();
                  break;
               case 's':
                    theTree.displayTree();
                  break;
               case 't':
                    System.out.print(" Enter type 1, 2 or 3 : ");
                    value = getInt();
                    theTree.traverse(value);
                  break;
               default:
                   System.out.print(" Invalid entry \n ");
             }
        }
   }

   public static String getString() throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        return s;
  }

  public static char getChar() throws IOException {
        String s = getString();
        return s.charAt(0);
  }

  public static int getInt() throws IOException {
       String s = getString();
       return Integer.parseInt(s);
  }
}

错误代码

Node.java:112: error: illegal start of expression
nBlanks / = 2 ;
          ^
1 error

Java运算符/=必须键入,中间不能有空格,否则它将被解析为2个独立的运算符/= ,这是语法错误。 尝试

nBlanks /= 2 ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM