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

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

    I am just going to offer a more reusable slice of code, its not the smallest but you can use it with any amount so its still worthy to be said

    health += amountToHeal;
    if (health >= 100) 
    { 
        health = 100;
    }
    

    You could also change the 100 to a maxHealth variable if you want to add stats to the game your making, so the whole method could be something like this

    private int maxHealth = 100;
    public void heal(int amountToHeal)
    {
        health += amountToHeal;
        if (health >= maxHealth) 
        { 
            health = maxHealth;
        }
    }
    

    EDIT

    For extra information

    You could do the same for when the player gets damaged, but you wouldn't need a minHealth because that would be 0 anyways. Doing it this way you would be able to damage and heal any amounts with the same code.

    0 讨论(0)
  • 2021-01-30 12:57

    Most simple way using the modulus operator.

    health = (health + 50) % 100;

    health will never equal or exceed 100.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-01-30 13:00

    I think an idiomatic, object oriented way of doing this is to have a setHealth on the Character class. The implementation of that method will look like this:

    public void setHealth(int newValue) {
        health = Math.max(0, Math.min(100, newValue))
    }
    

    This prevents the health from going below 0 or higher than 100, regardless of what you set it to.


    Your getHealed() implementation can just be this:

    public void getHealed() {
        setHealth(getHealth() + 15);
    }
    

    Whether it makes sense for the Character to have-a getHealed() method is an exercise left up to the reader :)

    0 讨论(0)
  • 2021-01-30 13:01

    I know this is a school project, but if you wanted to expand your game later on and be able to upgrade your healing power, write the function like so:

    public void getHealed(healthPWR) {
        health = Math.min(health + healthPWR, 100);
    }
    

    and call out the function:

    getHealed(15);
    getHealed(25);
    

    ...etc...

    Furthermore you can create your max HP by creating a variable that is not local to the function. Since I don't know what language you're using, I will not show an example because it might have the wrong syntax.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题