猿问

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

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


暮色呼如
浏览 209回答 2
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}
随时随地看视频慕课网APP

相关分类

Java
我要回答