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

前端 未结 14 1598
别跟我提以往
别跟我提以往 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:01

    I believe this will do

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

    Explanation:

    • If the gap for healing is 15 or less, health will become 100.

    • Otherwise if the gap is bigger than 15, it will add 15 to the health.

    So for example: if the health is 83, it will become 98 but not 100.

提交回复
热议问题