I am working on a simple video game program for school and I have created a method where the player gets 15 health points if that method is called. I have to keep the health at
I would just do this. It basically takes the minimum between 100 (the max health) and what the health would be with 15 extra points. It ensures that the user's health does not exceed 100.
public void getHealed() {
health = Math.min(health + 15, 100);
}
To ensure that hitpoints do not drop below zero, you can use a similar function: Math.max
.
public void takeDamage(int damage) {
if(damage > 0) {
health = Math.max(health - damage, 0);
}
}