在while循环中设置变量

0 java loops image colors while-loop

我正在创建一个项目,我需要创建一个方法,创建两个不同的紫色斑点,这些斑点将在不同的程序中调用.这是我的代码:

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;

}
Run Code Online (Sandbox Code Playgroud)

在我调用它的其他程序中,我有:

  FileChooser.pickMediaPath();
  PaintablePicture pRef;
  pRef = new PaintablePicture(FileChooser.pickAFile());
  pRef.purpleSplotch(10,20);
  pRef.explore();
Run Code Online (Sandbox Code Playgroud)

我必须做一个使用变量的while循环,以便制作splotches,我不明白帮助我,请得到"错误:重复局部变量x"

Abi*_*Abi 6

您在方法中传递值"x"和"y"

public void purpleSplotch(int x,int y)
Run Code Online (Sandbox Code Playgroud)

并在方法中再次在本地声明它

int x=0;
int y=1;
Run Code Online (Sandbox Code Playgroud)

这就是你得到那个错误的原因.

在方法内声明另一个变量而不是x和y.

进行以下更改:

public void purpleSplotch(int x,int y)
  {
  int x1=0;
  int y1=1;
  while(x1 < x*2)
  while(y1 < y*3)
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您能够解决重复变量错误,请接受答案 (2认同)