Java 减小“的大小”;如有其他",;环

Java 减小“的大小”;如有其他",;环,java,Java,我想知道是否有办法减少这个循环的大小。我正在以+10的增量寻找条件,然后相应地加载一个图像,同样是在前一个图像的+10的X,Y坐标中。提前谢谢 if (xpos >= 80){ image(imgRainCloud, 90, 10); } if (xpos >= 90){ image(imgBlock, 90, 10); image(imgRainCloud, 100, 10); } if(xpos >= 100){ image(imgBlo

我想知道是否有办法减少这个循环的大小。我正在以+10的增量寻找条件,然后相应地加载一个图像,同样是在前一个图像的+10的X,Y坐标中。提前谢谢

  if (xpos >= 80){
  image(imgRainCloud, 90, 10);
  }
  if (xpos >= 90){
  image(imgBlock, 90, 10);
  image(imgRainCloud, 100, 10);
  }
  if(xpos >= 100){
  image(imgBlock, 100, 10);
  image(imgRainCloud, 110, 10);
  }
  if(xpos >= 110){
  image(imgBlock, 110, 10);
  image(imgRainCloud, 120, 10);
  }
  if(xpos >= 120){
  image(imgBlock, 120, 10);
  image(imgRainCloud, 130, 10);
  }
  if(xpos >= 130){
  image(imgBlock, 130, 10);
  image(imgRainCloud, 140, 10);
  }
  if(xpos >= 140){
  image(imgBlock, 140, 10);
  image(imgRainCloud, 150, 10);
  }
  else(xpos < 80){
  image(imgBlock, 0, 0);
  image(imgRainCloud, 0, 0);
  }
if(xpos>=80){
图像(imgrainclud,90,10);
}
如果(xpos>=90){
图像(imgBlock,90,10);
图像(imgrainclud,100,10);
}
如果(xpos>=100){
图像(imgBlock,100,10);
图像(imgrainclud,110,10);
}
如果(xpos>=110){
图像(imgBlock,110,10);
图像(imgrainclud,120,10);
}
如果(xpos>=120){
图像(imgBlock,120,10);
图像(imgrainclud,130,10);
}
如果(xpos>=130){
图像(imgBlock,130,10);
图像(Imgrainclud,140,10);
}
如果(xpos>=140){
图像(imgBlock,140,10);
图像(imgrainclud,150,10);
}
其他(xpos<80){
图像(imgBlock,0,0);
图像(imgrainclud,0,0);
}

您可以将if和else分解为不同的集合 例如:


假设xpos是一个整数,并且假设您的代码示例缺少一组else语句,您可能会执行以下操作:

int range = xpos / 10;
if (range > 14)
    range = 14;
if (range >= 8)
{
    image(imgBlock, range*10, 10);
    image(imgRainCloud, (range+1)*10, 10);
}
else
{
    image(imgBlock, 0, 0);
    image(imgRainCloud, 0, 0);
}

您也可以除以10,然后使用switch语句。

if-else..
不是循环。我必须说,我既没有听说过“if-else循环”也没有听说过语言“C/Java”。您没有使用else语句,因此即使满足匹配条件,也会对每条if行进行求值。前两个条件使用
=
,其他人使用
。有意还是无意?无意,谢谢
if (xpos >= 80){
    image(imgRainCloud, 90, 10);

    for (int i = 90; xpos >= i; i+=10)
    {
        image(imgBlock, i, 10);
        image(imgRainCloud, i+10, 10);
    }
}
else
{
    image(imgBlock, 0, 0);
    image(imgRainCloud, 0, 0);
}
int range = xpos / 10;
if (range > 14)
    range = 14;
if (range >= 8)
{
    image(imgBlock, range*10, 10);
    image(imgRainCloud, (range+1)*10, 10);
}
else
{
    image(imgBlock, 0, 0);
    image(imgRainCloud, 0, 0);
}