你如何计算Java中的百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32050496/
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 do you calculate percentages in Java?
提问by draco miner
So I am trying to make a game with a health bar for the player but whenever I use cross products or any other method of math that should work the percentage result is 0.0
...
Here is the .java file that handles this:
所以我试图为玩家制作一个带有健康栏的游戏,但是每当我使用交叉产品或任何其他应该工作的数学方法时,百分比结果是0.0
......这是处理这个的 .java 文件:
package com.game.graphics;
import com.game.entity.mob.Mob;
public class HUD {
Mob target;
Sprite healthbar;
Sprite bgBar;
double percent_normal;
public HUD(Mob target) {
this.target = target;
bgBar = new Sprite(0xff777777, 104, 8);
healthbar = new Sprite(0xffFF0000, 100, 6);
}
public void update() {
percent_normal = target.getHealth() / 100;
percent_normal *= target.getMaxHealth();
}
public void printData() {
System.out.println("Normal(" + percent_normal + ")");
// if the targets's health is any lower than the max health then this seems to always return 0
}
public void render(Screen s) {
s.renderSprite(5, 5, bgBar, false);
s.renderSprite(7, 6, new Sprite(0xffFF0000, (int) percent_normal, 6), false);
}
}
I'm not sure if my Math is completely off or if I'm going about this horibly wrong. I've tried looking up how to calculate it to no avail so that's why I'm putting this question here.
我不确定我的数学是否完全关闭,或者我是否正在处理这个可怕的错误。我试过查找如何计算它无济于事,所以这就是我在这里提出这个问题的原因。
采纳答案by ThomasS
Your way of calculating the percentage seems to be off. To calculate the percentage of X compared to Y you have to do:
你计算百分比的方式似乎不对。要计算 X 与 Y 相比的百分比,您必须执行以下操作:
double result = X/Y;
result = result * 100;
So in this case your code should look like
所以在这种情况下你的代码应该看起来像
public void update() {
percent_normal = target.getHealth() / target.getMaxHealth();
percent_normal *= 100;
}
The reason you are always getting 0.0 although is because of the int 100. I believe the target.getHealth()/100
will return a value that is in the form of 0.x and will be turned into int 0 before being cast to double.
你总是得到 0.0 的原因是因为 int 100。我相信它target.getHealth()/100
会返回一个 0.x 形式的值,并且在被转换为双倍之前将被转换为 int 0。
回答by Kevin
Shouldn't it be this? :
不应该是这个吗?:
public void update() {
percent_normal = target.getHealth() / target.getMaxHealth();
percent_normal *= 100;
}
E.g. : Player has 120 health and maximum health is 180. This is, 120 / 180 = 0.66666666666
which gives in percentage 0.66666666666 * 100 = 66,66...%
instead of 120 / 100 = 1.2
and multiplying this by target.getMaxHealth()
which is 180, results in (1.2 * 180) > 100
.
例如:玩家有 120 生命值,最大生命值是 180。这是,120 / 180 = 0.66666666666
它以百分比给出,0.66666666666 * 100 = 66,66...%
而不是120 / 100 = 1.2
乘以target.getMaxHealth()
180,结果是(1.2 * 180) > 100
。
回答by Shiva
Basically to calculate percentage we use this,
基本上我们使用这个来计算百分比,
percentage = (a / b) * 100
百分比 = (a / b) * 100
So in your case update() method is wrong and replace with code below,
所以在你的情况下 update() 方法是错误的,用下面的代码替换,
public void update()
{
percent_normal = target.getHealth() / target.getMaxHealth();
percent_normal * = 100;
}
Here is an example, just for reference - http://www.flowerbrackets.com/java-calculate-percentage/
这是一个例子,仅供参考 - http://www.flowerbrackets.com/java-calculate-percentage/