How can I increment a variable without exceeding a maximum value?

前端 未结 14 1569
别跟我提以往
别跟我提以往 2021-01-30 12:03

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

14条回答
  •  [愿得一人]
    2021-01-30 13:00

    You don't need a separate case for each int above 85. Just have one else, so that if the health is already 86 or higher, then just set it directly to 100.

    if(health <= 85)
        health += 15;
    else
        health = 100;
    

提交回复
热议问题