当汽车离开时,汽车在车库内移动的次数应与车牌一起显示。我目前得到的输出显示一切正常,但所有汽车的移动次数保持为 0。我无法确定如何增加变量并在输出中显示累积值。我该如何解决?汽车类package garagetester;public class Car { private String licensePlate;//stores the license plate of the car as a String private int movesCount = 0;//stores the number of times the car has been //moved public Car(String licensePlate)//builds a Car object with { this.licensePlate = licensePlate; } public String getLicensePlate() { return licensePlate; } public int getMovesCount() { return movesCount; } public void incrementMovesCount(int movesCount) { movesCount++; }}//end of Car class
2 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
您的参数movesCount正在影响类成员movesCount。在以下突变器中:
public void incrementMovesCount(int movesCount) {
// movesCount++; --> this is incrementing the parameter
// either remove the parameter `movesCount` from this mutator
// since it's not being used, or do the following
this.movesCount++; // --> this refers to the class member movesCount
}
添加回答
举报
0/150
提交
取消