0
package data_structures;

import java.util.StringTokenizer;

public class ExpressionEvaluator implements Stack, Queue {
    private String userInput;
    public ExpressionEvaluator() {
        Userinput = null;
    }

    Iterator<E> st = new StringTokenizer();
    token = st.next();
    while (st.hasMoreTokens()) {

此时问题已经发生,所以我不再继续代码

错误:

data_structures/ExpressionEvaluator.java:10: illegal start of typejava
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: <identifier> expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: ';' expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: illegal start of type
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: <identifier> expected
while (st.hasMoreTokens()) {
^
data_structures/ExpressionEvaluator.java:10: ';' expected
while (st.hasMoreTokens()) {
4

3 回答 3

2

以下代码应在方法内。不是直接在类内。

Iterator<E> st = new StringTokenizer();
    token = st.next();
    while (st.hasMoreTokens()) {
于 2012-10-16T03:09:56.847 回答
1

本节

Iterator<E> st = new StringTokenizer();
token = st.next();
while (st.hasMoreTokens()) {

不是用方法或静态初始化器。所有代码必须出现在类内的方法中

于 2012-10-16T03:11:35.020 回答
0

如下更新您的构造函数,因为变量命名不正确:

  public ExpressionEvaluator() {
      userInput = null;
  }

在某些方法中移动下面的代码(更新)构造函数代码,因为它不能独立存在,例如

public void evaluate(){
   StringTokenizer st = new StringTokenizer(userInput);
   while (st.hasMoreTokens()) {
      String token = st.nextToken();
      //manage your processing logic here
   }
}
于 2012-10-16T03:16:51.437 回答