语法错误,告诉我它想要的和其他几件事情 [英] Syntax error, telling me it wants ; and several other things
问题描述
只是尝试运行一些我正在做的作业的代码。这可能很简单,但对于我的生活我无法弄清楚为什么我在第一行得到上面的错误
(公共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整数getEntry(整型索引)throws SimulationException {
如果(thelog.isEmpty()|| thelog.size()< ; index){
return null;
}
返回thelog.get(index);
}
public整数变体()throws SimulationException {
int old,recent = 0;
recent = thelog.get(0);
old = thelog.get(thelog.size-1);
返回近期;
}
public Integer numEntries(){
return counter;
}
}
SimulationException
正确定义: WaterLog {
私人整数大小;
private整数最大;
private ArrayList< Integer> theLog; //参数化你的列表
private int counter = 0;
public WaterLog(Integer windowSize,Integer maxEntry)//这是您正在寻找的行为
{
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区分大小写
返回null;
}
返回log.get(index);
}
public整数变体()throws SimulationException {
int old,recent = 0;
recent = theLog.get(0);
old = theLog.get(theLog.size() - 1); //再次,看表壳,也是大小是一个方法
返回近期;
}
public Integer numEntries(){
return counter;
}
}
查看评论我补充说。
编辑:为了进一步解释发生了什么,让我们来看看你在做什么。
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
。为了解决这个问题,我们允许他们在构造函数之外仍然定义,但在其中指定值,我们可以访问 windowSize
和 maxEntry
。
Just trying to run through some code for an assignment I'm doing. It is probably simple but for the life of me I can't figure out why I get the above error at the first line
(public WaterLog.......).
Later I want to pass it this line:
[ log = new WaterLog(8, damCapacity); ]
Any help would be appreciated, I am new to this sorry.
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;
}
}
Assuming SimulationException
is defined correctly:
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;
}
}
See the comments I added.
EDIT: To explain a bit further what was going on, let's take a look at what you were doing.
public class WaterLog(Integer windowSize, Integer maxEntry) {
private Integer size = windowSize;
private Integer max = maxEntry;
private ArrayList theLog(int windowSize);
private int counter = 0;
You seem to have confused a class with a constructor. The variables you defined were attributes, which was correct. You needed to use the syntax I showed in my answer to create a constructor. For that same reason, you don't have access to variables like windowSize
. To remedy this, we allow them to still be defined outside the constructor, but assigned values inside it, where we have access to windowSize
and maxEntry
.
这篇关于语法错误,告诉我它想要的和其他几件事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!