由于编程,我可能一直盯着我的屏幕,但我真的不明白这里发生了什么。
我正在尝试制作游戏并计算升级所需的进度百分比。以下是我的方法:
public static int currentLevelInt(Integer XP){
return (int)(1 + sqrt(1+8*XP/50))/2;
}
public static double currentLevelDouble(Integer XP){
return (1 + sqrt(1+8*XP/50))/2;
}
public static int getPercentage(Integer XP){
double currentLevel = currentLevelDouble(XP);
int nextLevel = currentLevelInt(XP)+1;
return (int)(100-((nextLevel-currentLevel)*100));
}
假设我有54个XP。那么这些是预期值:
currentLevelInt(54) = 2
currentLevelDouble(54) = 2.05241747
(int)(100-(((currentLevelInt(54)+1)-currentLevelDouble(54))*100)) = 5%
//+1 because I need the next Level
但这些是我返回的值:
currentLevelInt(54) = 2
currentLevelDouble(54) = 2.0
(int)(100-(((currentLevelInt(54)+1)-currentLevelDouble(54))*100)) = 0%
//+1 because I need the next Level
我不明白发生了什么事?我将双倍传递给双倍,为什么它会被舍入为2.0?