【问题标题】:Syntax error, telling me it wants ; and several other things语法错误,告诉我它想要;和其他几件事
【发布时间】:2014-04-06 14:09:52
【问题描述】:

只是想为我正在做的任务运行一些代码。这可能很简单,但对于我来说,我无法弄清楚为什么我会在第一行得到上述错误

(public WaterLog.......). 

稍后我想通过这一行:

[ log = new WaterLog(8, damCapacity); ]

任何帮助将不胜感激,对不起,我是新手。

public class WaterLog(Integer windowSize, Integer maxEntry) {

private Integer size = windowSize;
private Integer max = maxEntry;
private ArrayList theLog(int windowSize);
private int counter = 0;


public void addEntry(Integer newEntry) throws SimulationException {

    theLog.add(0, newEntry);
    counter++;

}

public Integer getEntry(Integer index) throws SimulationException {

    If (thelog.isEmpty() || thelog.size() < index) {
        return null;
    }
    return thelog.get(index);

}

public Integer variation() throws SimulationException {

    int old, recent = 0;
    recent = thelog.get(0);
    old = thelog.get(thelog.size-1);
    return recent-old;
}

public Integer numEntries() {

    return counter;

}

}

【问题讨论】:

  • 一个方法不能包含其他方法。也许你的意思是公共类 WaterLog ?
  • WaterLog 是类还是方法?
  • 它不是类声明,也不是构造函数,也不是方法。这是什么?
  • 你把class,const混在一起了
  • 是的,这是一门课,对不起,我编辑了。

标签: java eclipse


【解决方案1】:

假设SimulationException 定义正确:

class WaterLog{

private Integer size;
private Integer max ;
private ArrayList<Integer> theLog; //parameterize your lists
private int counter = 0;

public WaterLog(Integer windowSize, Integer maxEntry) //this is the behavior you were    looking for
{
this.size = windowSize;
this.max = maxEntry;
theLog = new ArrayList<Integer>(windowSize);
}

public void addEntry(Integer newEntry) throws SimulationException {

theLog.add(0, newEntry);
counter++;

}

public Integer getEntry(Integer index) throws SimulationException {

if (theLog.isEmpty() || theLog.size() < index) { //Java is case sensitive
    return null;
}
return theLog.get(index);

}

public Integer variation() throws SimulationException {

int old, recent = 0;
recent = theLog.get(0);
old = theLog.get(theLog.size()-1); //again, watch case, also size is a method
return recent-old;
}

public Integer numEntries() {

return counter;

}

}

查看我添加的 cmets。

编辑:为了进一步解释发生了什么,让我们看看你在做什么。

public class WaterLog(Integer windowSize, Integer maxEntry) {

private Integer size = windowSize;
private Integer max = maxEntry;
private ArrayList theLog(int windowSize);
private int counter = 0;

您似乎将类与构造函数混淆了。您定义的变量是属性,这是正确的。您需要使用我在答案中显示的语法来创建构造函数。出于同样的原因,您无权访问 windowSize 之类的变量。为了解决这个问题,我们允许它们仍然在构造函数之外定义,但在构造函数内部赋值,我们可以访问windowSizemaxEntry。 p>

【讨论】:

  • @Takendarkk 好吧,构造函数的东西非常糟糕,但我可以尝试......
  • 是的,我很高兴你这样做,因为我不知道从哪里开始。
  • @Takendarkk 那里,我尽力了。
【解决方案2】:

如果你想向这个类传递一些参数,你需要一个构造函数。默认情况下,每个类都带有一个默认构造函数——它在那里,你只是看不到它(但可以声明它)。然后你可以做一个重载的构造函数(它需要一些参数),这就是你想要的......

如果你有课

class WaterLog {
    // no constructor
}

上面真的是一个

class WaterLog {
   public WaterLog() {
      // this is the constructor - if you do not declare it its still here, you just dont see it. Ofcourse you have option to declare it.
   }
}

重载的构造函数是这样的

class WaterLog {
   public WaterLog() {
     //default constructor
   }
   public WaterLog(Integer int, String string, etc...) {
     //overloaded constructor
   }
}

以上是您将参数传递给此类构造函数所需要的。我不擅长解释事情,但如果您需要更多说明,请告诉我:)

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 2015-12-14
    • 2019-04-02
    • 2014-08-13
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多