簡體   English   中英

如何在不超過最大值的情況下增加變量?

[英]How can I increment a variable without exceeding a maximum value?

我正在為學校制作一個簡單的視頻游戲程序,我已經創建了一種方法,如果調用該方法,玩家將獲得15個健康點。 我必須保持最高100的健康狀態,並且我現在有限的編程能力,我正在做這樣的事情。

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

我理解我的語法並不完美,但我的問題是,這可能是一個更好的方法,因為我還必須對損傷點做類似的事情,而不是低於0。

這稱為飽和算術

我會這樣做的。 它基本上需要在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);
    }
}

只需為健康添加15,所以:

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

然而,正如bland所指出的,有時多線程(多個代碼塊同時執行),健康狀況在任何時候都超過100可能會導致問題,並且多次更改健康屬性也可能很糟糕。 在這種情況下,您可以這樣做,如其他答案中所述。

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

對於85以上的每個int ,您不需要單獨的大小寫。 只有一個else ,所以如果健康狀況已經是86或更高,那么只需將其直接設置為100

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

我認為一種慣用的,面向對象的方法是在Character類上使用setHealth 該方法的實現如下所示:

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

無論您將其設置為什么,這都可以防止健康狀況低於0或高於100。


你的getHealed()實現可以是這樣的:

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

無論它是有道理的Character有-A getHealed()方法是留給讀者的練習:)

我只是提供一個更可重復的代碼片段,它不是最小的但你可以使用任何數量,所以它仍然值得說

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。 通過這種方式,您可以使用相同的代碼損壞和治愈任何金額。

health = health < 85 ? health + 15 : 100;

我會在一個幫助器類中創建一個靜態方法。 這樣,您可以擁有一個通用方法,而不是為每個需要適合某些邊界的值重復代碼。 它將接受定義最小值和最大值的兩個值,以及要在該范圍內鉗制的第三個值。

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 );

我知道這是一個學校項目,但是如果你想在以后擴展你的游戲並且能夠升級你的治療能力,那么寫下這樣的函數:

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

並調出功能:

getHealed(15);
getHealed(25);

...等等...

此外,您可以通過創建非函數本地的變量來創建最大HP。 由於我不知道你正在使用什么語言,我不會展示一個例子,因為它可能有錯誤的語法。

也許這個?

public void getHealed()
{
  if (health <= 85)
  {
    health += 15;
  } else
  {
    health = 100;
  }
}

如果你想要厚顏無恥並且在一行上使用你的代碼,你可以使用三元運算符

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

請注意,由於(可以說)可讀性差,有些人會對此語法不滿意!

我相信這樣做

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

說明:

  • 如果愈合的差距是15或更小,健康將變為100。

  • 否則,如果差距大於15,它將增加15健康。

例如:如果健康狀況是83,那么它將變為98而不是100。

如果我想要線程安全,我會這樣做,而不是使用同步塊。

原子compareAndSet在沒有開銷的情況下實現了與synchronized相同的結果。

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));
}

最簡單的方法是使用模數運算符。

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

健康永遠不會超過100。

   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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM