java如何防止变量永远运行?
Java博士不会运行我的程序,因为我的变量永远在运行,我不知道如何防止这种情况发生。如果你能告诉我怎么解决,那就太好了。我是初学者,所以这对我来说都是陌生的
下面是该类的代码:
import java.awt.Color;
class PaintablePicture extends Picture {
public PaintablePicture(String fileName) {
super(fileName);
}
public void purpleSplotch(int x, int y) {
int x1 = 0;
int y1 = 1;
while (x1 < x * 2)
while (y1 < y * 3)
{
Color purple = new Color(175, 0, 175);
Pixel pixRef;
pixRef = this.getPixel(x, y);
pixRef.setColor(purple);
}
return;
}
}
然后这就是我运行它的方法(忽略其他类的所有艺术海龟的东西,在我开始绘制可绘制的图片之前,一切都很好)
public class GraffitiApp
{
public static void main(String[] a)
{
FileChooser.pickMediaPath();
PaintablePicture pRef;
pRef = new PaintablePicture(FileChooser.pickAFile());
pRef.purpleSplotch(14,14);
pRef.purpleSplotch(17,20);
ArtisticTurtle tRef = new ArtisticTurtle(pRef);
tRef.pentagon(20);
tRef.spiral(50);
tRef.penUp();
tRef.forward(-50);
tRef.turn(90);
tRef.penDown();
tRef.letterK(50);
tRef.penUp();
tRef.turn(-130);
tRef.forward(100);
tRef.turn(90);
tRef.forward(140);
tRef.penDown();
tRef.letterK(30);
tRef.penUp();
tRef.moveTo(486, 60);
tRef.penDown();
tRef.star(30);
tRef.penUp();
tRef.moveTo(159,122);
tRef.turn(30);
tRef.penDown();
tRef.star(60);
tRef.penUp();
tRef.moveTo(330,103);
tRef.turn(-67);
tRef.penDown();
tRef.pentagon(40);
tRef.penUp();
tRef.moveTo(471,158);
tRef.penDown();
tRef.spiral(35);
pRef.explore();
} }
# 1 楼答案
当然,它将永远运行
在方法调用中,x的值大于0
y1的while循环也是如此
您可能需要在purpleSplotch方法中增加x1和y1。我不知道它必须做什么,但条件将始终为真,因此无限循环
# 2 楼答案
您的问题是您没有增加
while
循环中用作索引的变量,因此循环无限期运行。增加两个变量(x1
和y1
),以确保在某个点退出这两个while循环。现在,x1
永远不会超过x
,y1
永远不会超过y
# 3 楼答案
我想应该是这样
在while循环中,循环变量
x1
和y1
从未更改,这意味着它们始终是0
和1
,将永远满足条件,这就是程序永远运行的原因在任何带有临时变量的循环中,都需要更改循环变量的值,以便在某个阶段满足条件
在这里,我们将增加循环中的
x1
和y1
变量,x1
循环中的retuen语句将导致循环在第一次执行后退出,必须将其删除另外,你总是得到像素(x,y),x和y的值在整个循环中保持不变,我想你需要的是pixles(x1,y1)