Java Math.pow 四舍五入为整数多次返回相同的数字

标签 java math exponent

我正在尝试制作唱首歌游戏,我希望机器人价格像 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/

相关文章:

java - 更改选择选项卡上的 JTabbedPane 组件

java - 如何修复 Hibernate "object references an unsaved transient instance - save the transient instance before flushing"错误

math - 为什么二进制减法总是(?)通过添加补码来完成?

java - 尝试用java编写科学计算器,但无法使指数按钮工作

R:如何在 n-choose-k 中计算大数?

java - 函数返回 NaN

java - 如果选中复选框,则将 JPanel 设置为可见

java - Spring自动从包中加载上下文文件

c++ - 近正弦数据下界的(线性或多项式)回归算法

php - 随机生成圆内坐标