你如何制作一个减去 2 个值但会使值 "a" 不低于 0 的函数?

How do you make a function that subtracts 2 values but will make value "a" not go beneath 0?

我想从饥饿值中减去食物值,但饥饿值不允许低于 0。我该如何实现? 我也在 Java

中编码
public void eat(Food food) {
        
        if (this.hunger > 0 && food.getTyp() == "Dogfood") {
            hunger = hunger - food.getVolume();
            
        }
        
    }
public void eat(Food food) {
    if ("Dogfood".equals(food.getTyp())) {
        hunger = hunger - food.getVolume();
        hunger = hunger < 0 ? 0 : hunger;
    }
}

像这样,每次给饥饿值设置一个新的值,就计算新的值。然后,如果饥饿 < 0,则将其设置为 0。 另外,我根据其中一条评论更改了您的 equals 方法。