Reputation: 1
I'm struggling to debug my code. There is this one error after getting my instance and trying to substract 1 from my instance variable the code give me an error. I tried to debug many times but I couldn't figure it out. The error is invalid arguement to operation ++/--
game.setNumHealthPotions(game.getNumHealthPotions()--); // The Second game.getNumHealhPotions has the error.
2nd error
game.setNumHealthPotions(game.getNumHealthPotions++);
My setters and getters both lines
public void setNumHealthPotions(int numHealthPotions){
this.numHealthPotions = 1;
}
public int getNumHealthPotions(){
return numHealthPotions;
}
Upvotes: 0
Views: 102
Reputation: 50726
++
and --
are assignment operators. You can't assign a value to a method. You'll have to call both get
and set
to read and assign:
game.setNumHealthPotions(game.getNumHealthPotions() + 1);
Or you can add an increment method if it's a common use case:
public int incrementNumHealthPotions(int increment) {
return this.numHealthPotions += increment;
}
Called like this:
game.incrementNumHealthPotions(-1);
Upvotes: 2