我正在制作一个非常基本的自动“Cookie Clicker”程序,我遇到了这行代码的问题:
z = Math.ceil(15*java.lang.Math.pow(1.15, x));
当在if语句中更新x时,这行代码似乎不理解并且z继续使用x的初始化值(为0)并保持为15(15 * 1.15 ^ 0)。
有谁知道该怎么做?
public class Tryout {
public static void main(String[] args) {
// TODO Auto-generated method stub
double x = 0; //Number of Cookie Clickers
double z; // Cost of the Cookie Clickers
double cookies = 0; //Amount of Cookies
double count = 0; //Additional amount of cookies per second from the Cookie Clickers
final double constant = 0.1; //Amount of CPS (cookies per second) you get from 1 Cookie Clicker
z = Math.ceil(15*java.lang.Math.pow(1.15, x)); //with 0 CC the cost is 15, with 1 CC the cost is 18, with 2 CC...
while (1>0) { // the game loop, (infinity loop)
if(cookies >= z) { //If you got enough cookies you autobuy a new CC and the number of CC's increase as well as the cost of a CC
System.out.println("cookies" + cookies); //Amount of cookies before you buy
System.out.println("You bought a new Cookie Clicker; +0.1 cookie each second!");
System.out.println(cookies + " - " + z + " = " + (cookies - z));
cookies = cookies - z;
x++;
}
try { //delayer (Uptades 1 time per second)
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
count = x*constant;
cookies = cookies + 1 + count;
Math.round(cookies);
System.out.println("cookies; " + cookies); // Your current amount of cookies
}
}
}
答案 0 :(得分:2)
该行代码只执行一次。将其移动到循环内,以便每次x更改时重新评估z。