我正在写一个简单的GCD函数,我相信它会一直返回一些东西,因为我在if中写了一个return语句。如何确保程序不会以编译错误结束。最后写另一个return语句可以吗?如何确保你在递归函数中返回一些东西?
private static int gcd(long a, long b){
if(b==0)
return (int)a;
else{
a=a%b;
gcd(b,a);
}
//I want to avoid the next statement as I think it is redundant,
//but the compiler does not allow me to skip it.
return 0;
}
如果正常执行意味着它不应该达到最终返回语句,那么您可以并且应该抛出异常。 – Michael
有一个@CheckReturnValue注释,[Google的errorprone](http://errorprone.info/bugpattern/CheckReturnValue)用来确保您使用返回值。 –
@AndyTurner根本不是他要求的 – Michael