是否可以使用java在if块中编写return语句?

时间:2015-02-03 10:00:29

标签: java arrays if-statement

我想根据if块返回Vector,但代码给出了以下错误:Add return statement

是否可以在if块中写入return语句?

public static  int[] zeroVectorBinning1( ImageFloat32 angle,ImageFloat32 Magnitude )
{
    for (int NumberOFChanks=0;NumberOFChanks<locations_original.size();NumberOFChanks++)
        {
            for(int i=0;i<angle.getHeight();i++)
                for(int j=0;j<angle.getWidth();j++)
            {
                int  orientaionVal=(int) angle.get(j, i);

                 if(orientaionVal<=0)
                    {int magnitudeVal=(int) Magnitude.get(j, i);
                    int[] Vector = new int[19];
                    Vector=zeroVector(19);
                    Vector[0]=magnitudeVal;
                    return Vector;
                    }
                else if(orientaionVal<=20)
                    {int magnitudeVal=(int) Magnitude.get(j, i);
                    int[] Vector = new int[19];
                    Vector=zeroVector(19);
                    Vector[1]=magnitudeVal;
                    return Vector;
                    }
                else(orientaionVal >= 0 && orientaionVal <=20)

                    {
                    int magnitudeVal=(int) Magnitude.get(j, i);
                    int[] Vector = new int[19];
                    Vector=zeroVector(19);
                    Vector[0]=magnitudeVal;
                    Vector[1]=magnitudeVal;
                    return Vector;

                    }

            }
        }

} 

7 个答案:

答案 0 :(得分:4)

是,

但是你的功能最终还是没有返回任何东西,所以你必须返回一些东西,甚至null

所以当你调用这个函数时,它应该看起来像这样:

int[] fuctionResult = zeroVectorBinning1(....);
if (fuctionResult != null){
....
}

答案 1 :(得分:3)

if块中使用return语句没有错,但是你的方法必须在任何执行路径中都有一个return语句。

您的for循环可能永远不会被执行(例如,如果locations_original.size()为0),在这种情况下,将不会到达包含return语句的if块。因此,您必须在循环后添加一个return语句。

答案 2 :(得分:1)

您可以通过两种方式解决此问题:

  • 最后要么在完成方法之前抛出异常。
  • 或者最后只返回null。

编译器抱怨的原因是,如果locations_original.size()返回0,那么此方法永远不会返回任何与您在方法中所说的将返回int数组相矛盾的内容。

答案 3 :(得分:0)

有可能。但是你需要为每个案例提供一个return语句。如果你的for循环从未执行过。

所以添加return null;在你的方法结束时。

答案 4 :(得分:0)

问题是您的if/else-if/else解析不完整,else的条件如下:

else(orientaionVal >= 0 && orientaionVal <=20)

意思?这很古怪。只需忽略上一个(orientaionVal >= 0 && orientaionVal <=20)句子的else条件(当orientaionVal020时,它在逻辑上不正确),或者没有默认值否则在范围内。

当在if句子的范围内返回时,我们必须确保在任何条件下都有返回,所以:

  • 从if-else范围
  • 中返回默认值
  • 或者确保条件判断完成并且在任何条件下都有回报。

所以

正确:

if(condition){
   return;
}
return;

正确:

if(condition){
   return;
}else if(anotherCondition){
   return;
}else{
   return;
}

错:

if(condition){
 return;
}
// no return ...

错:

if(condition){
 return;
}else if(anotherCodition){
 return;
}
// no return ...

答案 5 :(得分:0)

是的,这是可能的。请按照代码段进行说明。

public class TestSample {

    public static void main(String... w){
    int h = new TestSample().call();
        System.out.println(h);

    }

    public int call(){
        int j =0;
        for(int i=0;i<10;i++){
            if(i==5){
                return i;
            }
        }

        return j;
    }

}

这打印5。

答案 6 :(得分:0)

public int getValue(){
 final int value = 3;
 if(value==1){
  return 1;
 }
 else if(value==2){
  return 2;
 }
 else if(value==3){
  return 3;
 }
 else{
  return -1;
 }
// no return because "else" covers all cases
}

在这种情况下,你有一个&#34; else&#34;最后,每个案例都涵盖在内。但如果你离开别人......

public int getValue(){
 final int value = 3;
 if(value==1){
  return 1;
 }
 else if(value==2){
  return 2;
 }
 else if(value==3){
  return 3;
 }
return -1;
// ... you have to return something at the end of the method, because value could be >3 or < 1 and the code under the last else if will be executed
}
相关问题