【问题标题】:Why is class, interface, or enum expected?为什么需要类、接口或枚举?
【发布时间】:2014-10-08 23:00:10
【问题描述】:

如何以及为什么会出现编译器错误(“预期的类、接口或枚举”)?这是代码:

public class RolloverCounter{
    private int counter=0;
    private int max;
    public RolloverCounter(int a){
      if(a<0){
        max = a;
      }
    }
    public void increment()
    {
       for(int i=0;i<=max;i++)
       {
         counter++;
         System.out.println(counter);
         if(counter==max){
           counter=0;
         }
       }
    }
    public void decrement(){
        for(int i=0;i<=max;i++)
          counter--;
        if(counter<0)
        {
          counter=max;
        }
        System.out.println(counter);
   }
   public String toString() {
      return counter;
   }
   public void reset(){
      counter = 0;
   }
}
}

我做错了什么?

【问题讨论】:

  • 你能发布你的例外吗?

标签: java enums


【解决方案1】:

您的 toString() 方法没有返回 String

public String toString() {
  return counter;
}

应该是这样的

public String toString() {
    return String.valueOf(counter);
}

最后,您发布的代码中似乎有一个额外的右大括号(在末尾)。

【讨论】:

  • 谢谢,我要么在书中错过了,要么还没有涉及到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多