Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 环路出现故障。仅迭代一次,与计数无关 publicstaticvoidmain(字符串[]args) { Picture pictObj=新图片(“C:\\caterpillar.jpg”); 象形文字swapRGB(2); pictObj.show(); } 公共无效swapRGB(){ Pixel[]pixelArray=this.getPixels(); 像素=零; int old_green=0; int old_blue=0; int old_red=0; 对于(int i=0;i_Java_Loops - Fatal编程技术网

Java 环路出现故障。仅迭代一次,与计数无关 publicstaticvoidmain(字符串[]args) { Picture pictObj=新图片(“C:\\caterpillar.jpg”); 象形文字swapRGB(2); pictObj.show(); } 公共无效swapRGB(){ Pixel[]pixelArray=this.getPixels(); 像素=零; int old_green=0; int old_blue=0; int old_red=0; 对于(int i=0;i

Java 环路出现故障。仅迭代一次,与计数无关 publicstaticvoidmain(字符串[]args) { Picture pictObj=新图片(“C:\\caterpillar.jpg”); 象形文字swapRGB(2); pictObj.show(); } 公共无效swapRGB(){ Pixel[]pixelArray=this.getPixels(); 像素=零; int old_green=0; int old_blue=0; int old_red=0; 对于(int i=0;i,java,loops,Java,Loops,这个程序改变图片中像素的颜色;它交换红、绿、蓝的值。我的问题是,不管numswap值的数量多少,它只发生一次。 如果我调用函数swapRGB()或swapRGB(numswaps)两次,它会更改颜色,但我不希望更改颜色,它应该根据numswaps的数量进行更改。 函数swapRGB()和swapRGB(numswaps)都在同一个类中 谢谢。这不是一个真正的答案,但太长了,无法发表评论 您可以更改swapRGB(int numswaps)来执行此操作: public static void ma

这个程序改变图片中像素的颜色;它交换红、绿、蓝的值。我的问题是,不管numswap值的数量多少,它只发生一次。 如果我调用函数swapRGB()或swapRGB(numswaps)两次,它会更改颜色,但我不希望更改颜色,它应该根据numswaps的数量进行更改。 函数swapRGB()和swapRGB(numswaps)都在同一个类中


谢谢。

这不是一个真正的答案,但太长了,无法发表评论

您可以更改
swapRGB(int numswaps)
来执行此操作:

public static void main(String[] args) 
{

 Picture pictObj = new Picture("C:\\caterpillar.jpg");
 pictObj.swapRGB(2);
 pictObj.show();
}

public void swapRGB(){
  Pixel[] pixelArray = this.getPixels();
  Pixel pixel = null;
  int old_green = 0;
  int old_blue = 0;
  int old_red = 0;
  for(int i = 0; i < pixelArray.length;i++){
      pixel = pixelArray[i];
      old_green = pixel.getGreen();
      old_blue = pixel.getBlue();
      old_red = pixel.getRed();
      pixel.setRed(old_green);
      pixel.setGreen(old_blue);
      pixel.setBlue(old_red);
  }
}

public void swapRGB(int numswaps) {
    Pixel[] pixelArray = this.getPixels();
    Pixel pixel = null;
    int old_green = 0;
    int old_blue = 0;
    int old_red = 0;
    int count = 0;

    while(count < numswaps) {
        for(int i = 0; i < pixelArray.length; i++) {
                pixel = pixelArray[i];
                //getting the green, red and blue value of the pixels
                old_green = pixel.getGreen();
                old_blue = pixel.getBlue();
                old_red = pixel.getRed(); 
                //Swapping Values of colors
                pixel.setRed(old_green);
                pixel.setGreen(old_blue);
                pixel.setBlue(old_red);
                pixel = pixelArray[i];
                count ++;
            }
        }
public void swapRGB(int numswaps){
对于(int i=0;i

它的代码要少得多,而且这也意味着你只需要让
swapRGB()
按你想要的方式工作。

while(count
两次为什么?我的错。我在这里格式化代码时意外添加了它。它不是代码的一部分。你能添加代码(或接口)吗Pixel类?我试过了,但那需要太多的格式(这是一个巨大的类,我必须从我的大学网站下载,你没有访问权限),对不起。你的
getPixels
方法是做什么的?我猜如果你只得到一次迭代,那么你的pixelArray只得到一个值。这很好,但是你能告诉我我犯了什么错误吗。你在内部循环中增加了
count
for(I=…
),所以当这个循环结束时,
count
将(大概)比
numswaps
大得多,外部
while
循环将退出。你说得对。我在错误的位置增加了计数!
public void swapRGB(int numswaps) {
    for (int i = 0; i < numswaps; ++i) {
        swapRGB();
    }
}