我正在制作一个项目,我需要创建一个方法,该方法将创建两个不同的紫色 Blob ,这些 Blob 将在不同的程序中调用。 这是我的代码:
public class PaintablePicture extends Picture
{
public PaintablePicture(String fileName)
{super(fileName);}
public void purpleSplotch(int x,int y)
{
int x=0;
int y=1;
while(x < x*2)
while(y < y*3)
{
Color purple = new Color(175, 0, 175);
Pixel pixRef;
pixRef= this.getPixel(x,y);
pixRef.setColor(purple);
}
return;
}
在我调用它的另一个程序中,我有:
FileChooser.pickMediaPath();
PaintablePicture pRef;
pRef = new PaintablePicture(FileChooser.pickAFile());
pRef.purpleSplotch(10,20);
pRef.explore();
我必须创建一个使用变量的 while 循环才能制作 Blob ,但我不明白请帮助我,我得到“错误:重复的局部变量 x”
最佳答案
您正在方法中传递值“x”和“y”
public void purpleSplotch(int x,int y)
并再次在方法中本地声明它
int x=0;
int y=1;
这就是您收到该错误的原因。
在方法内声明另一个变量,而不是再次声明 x 和 y。
进行以下更改:
public void purpleSplotch(int x,int y)
{
int x1=0;
int y1=1;
while(x1 < x*2)
while(y1 < y*3)
}
关于java - 在 while 循环中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15214499/