Java Math.pow 四舍五入为整数 returns 同一个数多次

Java Math.pow rounded to integer returns the same number several times

我正在尝试制作唱首歌游戏,我希望机器人价格像饼干唱首歌游戏一样成倍增长。我尝试使用 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));