我试图制作点击游戏,我希望机器人的价格能够像饼干游戏那样倍增。我尝试使用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
等。 请帮忙。
答案 0 :(得分:1)
正如人们在评论中指出的那样,问题出现在这行代码robotPrice = 100 * (int)Math.pow(1.15, cps);
你拿1.15,把它提高到电源cps,然后切掉所有小数位。这只会给你一个整数,然后乘以100。
在删除所有小数之前,您希望将它乘以100。
robotPrice = (int)(100 * Math.pow(1.15, cps));