使用Eclipse IDE在Java中编程时,我有时会使用调试功能。很多时候我喜欢在程序运行的同时实时更改变量。但是,我经常发现更改某些变量实际上不会影响当前正在运行的程序。
我的问题是:是否有一些调试规则?哪些变量在调试器的范围内?
如果这是一个愚蠢的问题,我很抱歉。我对Eclipse中的调试和一般的编程都很陌生。
以下代码是一个示例。我很抱歉,如果它很难阅读或其他什么,但问题是:在Ball类中,每当我更改PARTICLES_PER_CLICK
或SPEED
等最终变量时,它们都会实时更新,我可以看到程序窗口中的差异,但是当我改变RADIUS
变量时,它什么都不做,即使它与其他两个变量在同一个类中。
public class Main {
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static Ball[] ball = new Ball[100000];
public Main() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
Display.setTitle("Game Engine");
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
boolean[] doOnce = new boolean[10];
boolean gravity = false;
while (!Display.isCloseRequested()) {
if (Mouse.isButtonDown(1) || Mouse.isButtonDown(1)) {
if (!doOnce[0]) {
Ball.createParticles();
doOnce[0]=true;
}
} else {
doOnce[0] = false;
}
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 1; i <= Ball.ballAmount; i++) {
ball[i].updatePosition(gravity);
if (Mouse.isButtonDown(0)) {
gravity = true;
} else {
gravity = false;
}
if (ball[i].position.x > 0 && ball[i].position.y > 0 && ball[i].position.x < WIDTH && ball[i].position.y < HEIGHT) {
glBegin(GL_TRIANGLE_FAN);
glVertex2d(ball[i].position.x, ball[i].position.y);
for(int u=0;u<=360;u+=5){
glVertex2d(ball[i].position.x+Math.cos(u)*Ball.RADIUS, ball[i].position.y+Math.sin(u)*Ball.RADIUS);
}
glEnd();
}
}
System.out.println("Particle Amount: " + Ball.ballAmount);
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public static void main(String[] args) {
new Main();
}
}
class Ball {
public Vector2f position, velocity;
public static final int RADIUS = 10;
public static final int INITIAL_SPEED = 5;
public static final int SPEED = 2;
public static final int PARTICLES_PER_CLICK = 50;
public static int ballAmount = 0;
public double r, g, b;
public static Random rnd = new Random();
public Ball(double x, double y) {
int angle = rnd.nextInt(360);
position = new Vector2f((float) x, (float) y);
velocity = new Vector2f((float) Math.cos(angle) * rnd.nextFloat() * INITIAL_SPEED, (float) Math.sin(angle) * rnd.nextFloat() * INITIAL_SPEED);
this.r = rnd.nextDouble();
this.g = rnd.nextDouble();
this.b = rnd.nextDouble();
}
public void updatePosition(boolean gravity) {
this.position.x += this.velocity.x * SPEED;
this.position.y += this.velocity.y * SPEED;
if (gravity) {
double dx = this.position.x - Mouse.getX();
double dy = this.position.y - (Main.HEIGHT - Mouse.getY());
double distance = Math.sqrt(dx * dx + dy * dy);
this.velocity.x -= (this.position.x - Mouse.getX()) / distance;
this.velocity.y -= (this.position.y - (Main.HEIGHT - Mouse.getY())) / distance;
} else {
this.velocity.x *= 0.99;
this.velocity.y *= 0.99;
}
}
public static void createParticles() {
for (int i = 1; i <= PARTICLES_PER_CLICK; i++) {
ballAmount += 1;
Main.ball[ballAmount] = new Ball(Mouse.getX(),Main.HEIGHT- Mouse.getY());
}
}
}
答案 0 :(得分:1)
如果优化器看到最终变量,它知道它的值不会改变。它对知识的作用取决于它。它可能什么都不做(就像在你的情况下似乎发生了PARTICLES_PER_CLICK或SPEED),或者它可能只是将所有出现的变量替换为实际值。除了不更改最终变量的值之外,没有特殊规则。