为了账号安全,请及时绑定邮箱和手机立即绑定

Java - 每次循环迭代后累加器变量不会增加

Java - 每次循环迭代后累加器变量不会增加

暮色呼如 2021-09-29 17:48:36
当汽车离开时,汽车在车库内移动的次数应与车牌一起显示。我目前得到的输出显示一切正常,但所有汽车的移动次数保持为 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

}


查看完整回答
反对 回复 2021-09-29
  • 2 回答
  • 0 关注
  • 208 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信