这是我的代码:
public static Picture modifyPicture (Picture p, int value)
{
// get width and height of the picture
int width = p.getWidth();
int height = p.getHeight();
System.out.println ("Picture has width of " + width +
" and height of " + height);
Picture p2 = new Picture (width, height*value);
int x = -1;
int y = -1;
for ( x = 0 ; x < width ; ++x )
{
for ( y = 0 ; y < height ; ++y )
{
Pixel pixel1 = p.getPixel (x,y);
Color c1 = pixel1.getColor();
Pixel pixel4 = p2.getPixel (x, y);
pixel4.setColor( c1 );
Pixel pixel5 = p2.getPixel (x, y + 1 * height);
pixel5.setColor( c1 );
Pixel pixel6 = p2.getPixel (x, y + 2 * height);
pixel6.setColor( c1 );
}
}
return p2;
} // end of method
嘿伙计们,这似乎是一个简单的问题,但我不知道如何做到这一点。我想以某种方式循环 for 循环内的项目。该代码的作用是将图片垂直放置在彼此的顶部。此代码适用于 3 张图片(值 = 3),但如果我想要更少或更多图片,我必须不断添加/删除新行,即 Pixel Pixel7 等。
我需要一种根据 value 中的数字来循环它的方法。我不是在找你为我编写代码,只是给我一些想法如何做到这一点。感谢您的时间和帮助!
最佳答案
您必须修改方法以对每个像素循环 value
次:
public static Picture modifyPicture (Picture p, int value)
{
// get width and height of the picture
int width = p.getWidth();
int height = p.getHeight();
System.out.println ("Picture has width of " + width +
" and height of " + height);
Picture p2 = new Picture (width, height*value);
for (int x = 0 ; x < width ; ++x )
{
for (int y = 0 ; y < height ; ++y )
{
Pixel pixel1 = p.getPixel (x,y);
Color c1 = pixel1.getColor();
for (int j=1; j < value; j++) {
Pixel pixel = p2.getPixel (x, y + j * height);
pixel.setColor( c1 );
}
}
}
return p2;
} // end of method
关于java - 使用 Java 中的输入创建 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19695687/