该方法是记录玩家在完成一个洞时所用的笔画数。成功记录笔划时,该方法返回true。有两个注意事项:(1)必须按顺序记录孔并且必须从1开始。如果无序接收孔,则不记录分数并返回false(2)孔编号也必须有效。您不能记录小于1或大于球场上的洞数的洞的分数。调试我的代码时会给出java.lang.NullPointerException。这是什么意思,我该如何解决这个问题。
注意:holesPlayed是一个赋值为0
的实例变量这就是我所拥有的:
public boolean recordStrokes(int holeNumber, int strokes) {
if ((holeNumber >= 1) &&
(holeNumber <= this.holesPlayed) &&
(holeNumber == holesPlayed + 1
{
scores[holeNumber -1] = strokes;
holesPlayed = holesPlayed + 1;
return true;
}
else {
return false;
}
}
答案 0 :(得分:0)
您错过了IF条件的右括号
public boolean recordStrokes(int holeNumber, int strokes) {
if ((holeNumber >= 1) &&
(holeNumber <= this.holesPlayed) &&
(holeNumber == holesPlayed + 1 )) <-- this brackets are missing in your code
{
scores[holeNumber -1] = strokes;
holesPlayed = holesPlayed + 1;
return true;
}
else
{
return false;
}
}
答案 1 :(得分:0)
我无法确定,但我会猜测scores
从未初始化。你是用
scores = new int[NUM_HOLES];