2013-03-05 80 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

相關問題