简体   繁体   中英

Missing return statement in a method for loop

Here is my code:

//method to rotate the picture
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);
 if (value == 1)
 {
  Picture p2 = new Picture (height, width);

  int x = -1;
  int y = -1;

  for  ( x = 0 ; x < width ;  ++x )
  {
   for ( y = 0 ; y < height ; ++y )
   {
     // access the original pixel
     Pixel pixel1 = p.getPixel (x, y);
     Color c1 = pixel1.getColor();

     // access the pixel to modify
     int modifyXPos = (height-1)-y;
     int modifyYPos = x;
     Pixel pixel4 = p2.getPixel (modifyXPos, modifyYPos);
     pixel4.setColor( c1 );
   }
  }
  return p2;
 }
 else if (value == 2)
 {
  Picture p2 = new Picture ( width , height);

  int x = -1;
  int y = -1;

  for  ( x = 0 ; x < width ;  ++x )
  {
   for ( y = 0 ; y < height ; ++y )
   {
     // access the original pixel
     Pixel pixel1 = p.getPixel (x, y);
     Color c1 = pixel1.getColor();

     // access the pixel to modify
     int modifyXPos = x;
     int modifyYPos = (height-1) - y;
     Pixel pixel4 = p2.getPixel (modifyXPos, modifyYPos);
     pixel4.setColor( c1 );
   }
  }
  return p2;
 }
 else
 {
  System.out.println ("Value out of range");
 }
}

}//end of class

So at the second to last semicolon I get the error "missing return statement," and I understand why. I just have no idea how I would fix it. Rewriting Picture p2 etc before the "if" statements would be useless because the coordinates have to change, so other than that, I don't see how I could put a return statement at the end. Please help, and thank you for your time and answers!

It says missing return statement because your method name

public static Picture modifyPicture (Picture p, int value)

says that you are going to return a Picture object, but you are not in one of the "else" cases,

else
{
   System.out.println ("Value out of range");
   return null;     // Notice this
}

thus add a return statment there

If you promise to return a Picture , you must return a Picture or throw an exception.

At the end of your method you can add

return null;

Or you can throw an exception at then end.

throw new IllegalArgumentException("Value out of range");

If you don't know anything about exceptions, use return null , and then go read about exceptions and error handling in Java.

请在else循环中添加return语句

Hey you have missed return statement for else block. If No condition is satisfied then nothing is returned.Include return in else block.

Try returning empty object.

If it's absolutely necessary that the coordinates are changed, then it's necessary your first code block gets executed and the 'else' branch never gets triggerd, right? Then, in the else clause, throw an argumentException. Or, perhaps better, remove the ELSE and end the function by throwing the ArgumentException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM