我正在尝试制作唱首歌游戏,我希望机器人价格像 cookies 唱首歌游戏一样成倍增长。我尝试使用 cookie Clicker 的价格计算公式 ( http://cookieclicker.wikia.com/wiki/Building )。
if (cookies >= robotPrice) {
cookies -= robotPrice;
cps ++;
//Here is the algorithm
robotPrice = 100 * (int)Math.pow(1.15, cps);
System.out.println("robotPrice set to " + robotPrice);
}
但是当我运行程序时,我得到以下输出:
robotPrice set to 100
robotPrice set to 100
robotPrice set to 100
robotPrice set to 100
robotPrice set to 200
robotPrice set to 200
robotPrice set to 200
robotPrice set to 300
robotPrice set to 300
等 请帮忙。
最佳答案
正如人们在评论中指出的那样,问题出现在这行代码 robotPrice = 100 * (int)Math.pow(1.15, cps);
您正在取 1.15,将其提高到 cps 次方,然后去掉所有小数位。这只会给你一个整数,然后乘以 100。
在删除所有小数之前,您想将其乘以 100。
robotPrice = (int)(100 * Math.pow(1.15, cps));
关于Java Math.pow 四舍五入为整数多次返回相同的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875457/