Java 如何在不超过最大值的情况下增加变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18647214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I increment a variable without exceeding a maximum value?
提问by Steven Eck
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.
我正在为学校开发一个简单的视频游戏程序,我创建了一个方法,如果调用该方法,玩家将获得 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
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.
我知道我的语法并不完美,但我的问题是,有什么更好的方法可以做到,因为我也必须对伤害点做类似的事情,而不是低于 0。
This is called saturation arithmetic.
这称为饱和算法。
采纳答案by Chris Forrence
I would just do this. It basically takes the minimum between 100 (the max health) and what the health would be with 15 extra points. It ensures that the user's health does not exceed 100.
我只会这样做。它基本上需要 100(最大生命值)和 15 点额外生命值之间的最小值。它确保用户的健康不超过100。
public void getHealed() {
health = Math.min(health + 15, 100);
}
To ensure that hitpoints do not drop below zero, you can use a similar function: Math.max
.
为了确保生命值不低于零,则可以使用类似的功能:Math.max
。
public void takeDamage(int damage) {
if(damage > 0) {
health = Math.max(health - damage, 0);
}
}
回答by rgettman
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
.
您不需要为int
上面的每个单独的案例85
。只要有一个else
,这样如果健康状况已经86
或更高,那么只需将其直接设置为100
。
if(health <= 85)
health += 15;
else
health = 100;
回答by Jordan
just add 15 to the health, so:
只需在健康上加 15,所以:
health += 15;
if(health > 100){
health = 100;
}
However, as bland has noted, sometimes with multi-threading (multiple blocks of code executing at once) having the health go over 100 at anypoint can cause problems, and changing the health property multiple times can also be bad. In that case, you could do this, as mentioned in other answers.
然而,正如 bland 所指出的,有时在多线程(同时执行多个代码块)的情况下,健康状况在任何时候都超过 100会导致问题,多次更改健康状况属性也可能很糟糕。在这种情况下,您可以这样做,如其他答案中所述。
if(health + 15 > 100) {
health = 100;
} else {
health += 15;
}
回答by Juto
Maybe this?
也许这个?
public void getHealed()
{
if (health <= 85)
{
health += 15;
} else
{
health = 100;
}
}
回答by Daniel Kaplan
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:
我认为这样做的惯用的、面向对象的方法是setHealth
在Character
类上有一个。该方法的实现如下所示:
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.
这可以防止生命值低于 0 或高于 100,无论您将其设置为什么。
Your getHealed()
implementation can just be this:
您的getHealed()
实现可以是这样的:
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 :)
Character
有一个getHealed()
方法是否有意义是留给读者的练习:)
回答by MarmiK
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.
如果治疗差距为 15 或更少,则生命值将变为 100。
否则,如果差距大于 15,则生命值将增加 15。
So for example: if the health is 83, it will become 98 but not 100.
例如:如果生命值为 83,它将变为 98 而不是 100。
回答by Oleksiy
If you want to be cheeky and fit your code on one line, you could use a ternary operator:
如果你想厚颜无耻地把你的代码放在一行上,你可以使用三元运算符:
health += (health <= 85) ? 15 : (100 - health);
Note that some people will frown upon this syntax due to (arguably) bad readability!
请注意,由于(可以说)可读性差,有些人会对这种语法不屑一顾!
回答by 5tar-Kaster
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
如果您想将统计数据添加到您制作的游戏中,您也可以将 100 更改为 maxHealth 变量,因此整个方法可能是这样的
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.
当玩家受到伤害时,您可以做同样的事情,但您不需要 minHealth,因为无论如何它都是 0。这样做,您将能够使用相同的代码损坏和治愈任何数量。
回答by kiss my armpit
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;
}
回答by BlackBeltScripting
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.
此外,您可以通过创建一个非函数局部变量来创建最大 HP。由于我不知道您使用的是哪种语言,因此我不会展示示例,因为它可能具有错误的语法。