2013-03-05 79 views
0

Dr.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

在while循环中从不使用/增加'x1'和'y1'值 – 2013-03-05 02:55:06

+1

在发布到任何位置之前设置代码的格式。 Eclipse - >'Ctrl' +'Shift' +'F',NetBeans - >'Alt' +'Shift' +'F' – Pshemo 2013-03-05 02:55:11

回答

0

你的问题是,你不增加作为指标变量在你的while循环中,所以循环无限期地运行。增加两个变量(x1y1)以确保您在某个时间点将会退出while循环。目前,x1永远不会超过x,y1永远不会超过y

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); 
     //increment y1 here 
     y1++; 
    } 
    //increment x1 here 
    x1++; 
} 
+0

颜色不会变成紫色,它不会显示在我的图片上, – neech 2013-03-05 03:29:18

1

我觉得应该是

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; 
       // get pixels (x1, y1) instead of (x, y) there is use to set the color of the same pixel(x,y) in a loop 
       pixRef = this.getPixel(x1, y1); 
       pixRef.setColor(purple); 
       // increment y1 
       y1++; 

      } 
     // increment x1 
     x1++; 
    } 
} 

在while循环的循环变量x1y1永远不会改变,这意味着他们总是01这将永远满足的条件,即为什么你的程序永远在运行。

在任何带有temp变量的循环中,您需要更改循环变量的值,以便在某个阶段满足条件。

这里我们将在其循环中增加x1y1变量,x1循环中的retuen语句将导致循环在第一次执行后退出,必须将其删除。

你也都拿到像素(X,Y)总是,x和y的值保持恒定,通过了循环,我想你所需要的就是pixles(X1,Y1)

0

当然它将与具有值大于0 x作为在调用所述方法

pRef.purpleSplotch(14,14); 

相同永远

while(x1 < x*2) 

运行与Y1 while循环发生。

您可能需要在purpleSplotch方法中增加x1和y1。我不知道该怎么做,但条件永远是真的,因此是无限循环。

+0

嗨,我纠正它,但现在我的污点不会出现,我很伤心 – neech 2013-03-05 03:22:47

相关问题