这是一个非常简单的 Java 代码,我想编写一个代码来计算 step 方法被调用的次数。本质上,此代码将绘制一个 blob 并计算 blob 需要多少“步骤”。如果步数等于 Max,则 blob 将采用新的 dx/dy,例如新的速度。
Blob 类不是那么重要,所以我没有附上它。但是,PurposefulWanderer 类被另一个类调用,特别是 GUI
我尝试创建一个名为 current 的静态变量,并在 step 方法下将其加一,但这不起作用。它仍然说当前变量仍然是 0。
public class PurposefulWanderer extends Blob {
private int TOTAL;
private static int current = 0;
public PurposefulWanderer (double x, double y) {
super (x, y);
this.TOTAL = (int) (Math.random()*10)+10;
}
@Override
public void step() {
++current;
// Choose a new step between -1 and +1 in each of x and y
if (current == this.TOTAL)
dx = 2 * (Math.random()-0.5);
dy = 2 * (Math.random()-0.5);
x += dx;
y += dy;
current = 0
}
}