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

标签 java

我对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 ;

关于java - Java 错误中表达式的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587152/

相关文章:

java - JUnit 命令行测试

java - 单击按钮时关闭特定的 JInternalFrame

java - 如何从代码中获取 Java AWS SDK 版本?

java - JTable 不会失去对 ENTER 和 TAB 的关注

java - 配置 Jetty、Jersey 和 Guice

java - 从 JSON 文件中获取数据,保存为对象并使用键值获取数据

java - 实现一个也被覆盖的方法

java - 多线程程序在不同操作系统中的行为

java - 如何存储包含用户输入值的数组

Java,如何检查两个二维数组是否包含相同的值