[英]Creating a for loop with inputs in Java
Here is my code: 这是我的代码:
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
Hey guys, this seems like a simple problem but I cannot figure out how I could do this. 大家好,这似乎是一个简单的问题,但是我不知道该怎么做。 I want to somehow loop the items inside of the for loop.
我想以某种方式循环for循环内的项目。 What the code does is places pictures vertically on top of each other.
该代码的作用是将图片垂直放置。 This code works for 3 pictures (value = 3), but if I wanted less or more pictures, I have to keep adding/removing a new line ie Pixel pixel7 etc.
该代码适用于3张图片(值= 3),但是如果我想要更少或更多的图片,则必须继续添加/删除新行,即Pixel pixel7等。
I need a way to loop this according to the number in value. 我需要一种根据值的数量循环的方法。 I'm not looking for you to write my code for me, just give me some idea how I could do this.
我不是要您为我编写代码,只是要告诉我一些如何做的方法。 Thank you for your time and help!
感谢您的时间和帮助!
You have to modify your method to loop value
times for each pixel: 您必须修改方法以循环每个像素的
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.