我在我的cs课上尝试了一些东西,然后突然我遇到了一个有趣的问题。
这是我的主要代码,
public void run(){
setSize(800, 600);
for(int i=0; i<= 30; i++){
elips el = new elips();
el.setFilled(true);
el.setColor(Color.RED);
elipsler.add(el);
add(el);
}
while(!stopthat){
for(int i=0; i< elipsler.size() -1; i++){
elipsler.get(i).cdRemove();
println("asd");
if(elipsler.get(i).canRemove == true){
remove(elipsler.get(i));
elipsler.remove(i);
elips el = new elips();
el.setFilled(true);
add(el);
elipsler.add(el);
}
}
}
}
这就是我的椭圆类。
public class elips extends GOval{
static int x, y, w, h;
int cd;
public boolean canRemove = false;
Random rand = new Random();
public elips(){
super(x, y, w, h);
canRemove = false;
cd = rand.nextInt(100);
x = rand.nextInt(780) + 20;
y = rand.nextInt(580) + 20;
w = rand.nextInt(80) + 20;
h = rand.nextInt(80) + 20;
}
public void cdRemove(){
if(this.cd <= 0){
this.canRemove = true;
}else{
this.cd--;
}
}
}
如您所见,我正在创建椭圆并给它们“移除冷却时间”,并且在冷却结束后椭圆会销毁。问题是如果我删除 println("asd") 行,代码将无法正常工作。也就是说,如果我删除那条线,椭圆会同时出现和消失(冷却不起作用)。
所以我想知道“println”行如何解决这个问题?