89

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 a max of 100 and with my limited programming ability at this point I am doing something like this.

public void getHealed(){
    if(health <= 85)
        health += 15;
    else if(health == 86)
        health += 14;
    else if(health == 87)
    health += 13; 
}// this would continue so that I would never go over 100

I understand my syntax about isn't perfect but my question is, what may be a better way to do it, because I also have to do a similar thing with the damage points and not go below 0.

This is called saturation arithmetic.

4

14 回答 14

226

我会这样做。它基本上需要 100(最大生命值)和 15 额外点的生命值之间的最小值。它确保用户的健康不超过100。

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

为确保生命值不低于零,您可以使用类似的功能Math.max

public void takeDamage(int damage) {
    if(damage > 0) {
        health = Math.max(health - damage, 0);
    }
}
于 2013-09-05T22:54:40.367 回答
70

只需将 15 添加到健康,因此:

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

然而,正如 bland 所指出的,有时在多线程(同时执行多个代码块)的情况下,健康状况在任何时候都超过 100 可能会导致问题,并且多次更改健康属性也可能很糟糕。在这种情况下,您可以这样做,如其他答案中所述。

if(health + 15 > 100) {
    health = 100;
} else {
    health += 15;
}
于 2013-09-05T22:54:56.810 回答
44

您不需要为int上述每个单独的案例85。只要有一个else,如果健康已经86或更高,则直接将其设置为100

if(health <= 85)
    health += 15;
else
    health = 100;
于 2013-09-05T22:54:46.200 回答
37

我认为一种惯用的、面向对象的方法是setHealthCharacter课堂上有一个。该方法的实现将如下所示:

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

这可以防止生命值低于 0 或高于 100,无论您将其设置为什么。


您的getHealed()实现可以是这样的:

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

Characterto have-a方法是否有意义getHealed()是留给读者的练习:)

于 2013-09-05T23:03:51.133 回答
14

我只是要提供一个更可重用的代码片段,它不是最小的,但你可以随意使用它,所以它仍然值得一说

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

如果您想将统计数据添加到您制作的游戏中,您还可以将 100 更改为 maxHealth 变量,因此整个方法可能是这样的

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

编辑

如需额外信息

当玩家受到伤害时,您也可以这样做,但您不需要 minHealth,因为无论如何它都是 0。这样做,您将能够使用相同的代码损坏和治愈任何数量。

于 2013-09-06T06:09:19.890 回答
10
health = health < 85 ? health + 15 : 100;
于 2013-09-08T17:06:44.310 回答
4

我会在帮助类中创建一个静态方法。这样,您可以拥有一个通用方法,而不是为需要适合某些边界的每个值重复代码。它将接受定义最小值和最大值的两个值,以及要限制在该范围内的第三个值。

class HelperClass
{
    // Some other methods

    public static int clamp( int min, int max, int value )
    {
        if( value > max )
            return max;
        else if( value < min )
            return min;
        else
            return value;
    }
}

对于您的情况,您可以在某处声明您的最低和最高健康状况。

final int HealthMin = 0;
final int HealthMax = 100;

然后调用传入您的最小值、最大值和调整后的健康状况的函数。

health = HelperClass.clamp( HealthMin, HealthMax, health + 15 );
于 2013-09-10T21:15:26.850 回答
3

我知道这是一个学校项目,但如果你想稍后扩展你的游戏并能够升级你的治疗能力,请编写如下函数:

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

并调用函数:

getHealed(15);
getHealed(25);

...ETC...

此外,您可以通过创建非函数本地变量来创建最大 HP。由于我不知道您使用的是什么语言,因此我不会显示示例,因为它可能有错误的语法。

于 2013-09-07T05:24:58.613 回答
2

如果您想厚脸皮并将代码放在一行中,则可以使用三元运算符

health += (health <= 85) ? 15 : (100 - health);

请注意,由于(可以说)可读性差,有些人会不赞成这种语法!

于 2013-09-06T04:48:08.243 回答
2

也许这个?

public void getHealed()
{
  if (health <= 85)
  {
    health += 15;
  } else
  {
    health = 100;
  }
}
于 2013-09-05T23:00:28.570 回答
1

如果我想成为线程安全的,我会这样做而不是使用同步块。

原子 compareAndSet 实现了与同步相同的结果,而没有开销。

AtomicInteger health = new AtomicInteger();

public void addHealth(int value)
{
    int original = 0;
    int newValue = 0;
    do
    {
        original = health.get();
        newValue = Math.min(100, original + value);
    }
    while (!health.compareAndSet(original, newValue));
}
于 2013-09-11T00:06:09.593 回答
1

我相信这会做

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

解释:

  • 如果治疗的差距为 15 或更少,则生命值将变为 100。

  • 否则,如果差距大于 15,则会增加 15 的生命值。

例如:如果健康为 83,它将变为 98 而不是 100。

于 2013-09-06T04:32:58.040 回答
1

使用模数运算符的最简单方法。

健康=(健康+ 50)%100;

健康永远不会等于或超过 100。

于 2015-11-16T22:20:01.053 回答
0
   private int health;
    public void Heal()
    {
        if (health > 85)
            health = 100;
        else
            health += 15;
    }
    public void Damage()
    {
        if (health < 15)
            health = 0;
        else
            health -= 15;
    }
于 2013-09-06T09:05:19.047 回答