Java 语法错误,告诉我它想要什么;还有其他一些事情
只是试着为我正在做的作业运行一些代码。这可能很简单,但就我个人而言,我不明白为什么我会在第一行出现上述错误Java 语法错误,告诉我它想要什么;还有其他一些事情,java,eclipse,Java,Eclipse,只是试着为我正在做的作业运行一些代码。这可能很简单,但就我个人而言,我不明白为什么我会在第一行出现上述错误 (public WaterLog.......). 稍后我想通过这行: [ log = new WaterLog(8, damCapacity); ] 任何帮助都将不胜感激,我是新来的抱歉 public class WaterLog(Integer windowSize, Integer maxEntry) { private Integer size = windowSize;
(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;
}
}
公共类滑铁卢(整数窗口大小,整数maxEntry){
私有整数大小=窗口大小;
私有整数max=maxEntry;
private ArrayList theLog(int windowSize);
专用整数计数器=0;
public void addEntry(整数newEntry)抛出SimulationException{
添加日志(0,新条目);
计数器++;
}
公共整数getEntry(整数索引)引发SimulationException{
If(thelog.isEmpty()| | thelog.size()<索引){
返回null;
}
返回log.get(索引);
}
public Integer variation()引发SimulationException{
int旧的,最近的=0;
最近=日志获取(0);
old=log.get(log.size-1);
回归近老;
}
公共整数numEntries(){
返回计数器;
}
}
假设正确定义了模拟异常
:
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;
}
}
您似乎将类与构造函数混淆了。您定义的变量是属性,这是正确的。您需要使用我在回答中显示的语法来创建构造函数。出于同样的原因,您无法访问诸如
windowSize
之类的变量。为了解决这个问题,我们允许它们仍然在构造函数外部定义,但在构造函数内部赋值,在这里我们可以访问windowSize
和maxEntry
,如果您想将一些参数传递给这个类,您需要一个构造函数。默认情况下,每个类都有一个默认构造函数——在那里,您只是看不到它(但可以声明它)。然后你可以做一个重载的construcotr(它需要一些参数),这就是你想要的
如果你有课
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
}
}
以上是向这个类构造函数传递参数所需要的。我不擅长解释问题,但如果您需要更多说明,请告诉我:)一个方法不能包含其他方法。。也许你的意思是公共类滑铁卢?滑铁卢是一个类还是一个方法?它不是一个类声明,不是一个构造函数,也不是一个方法。这是什么?你混淆了类,const在一起是的,这是一个类,我编辑了它很抱歉。@Takendark好吧,构造函数的东西太离谱了,但我可以试试……耶,我很高兴你这么做,因为我不知道从哪里开始。@Takendark,我已经尽力了。
class WaterLog {
public WaterLog() {
//default constructor
}
public WaterLog(Integer int, String string, etc...) {
//overloaded constructor
}
}