[英]Why does this not always have a return statement
private static BigInteger[] matrixPow(BigInteger[] matrix, int n){
if(n==0){
BigInteger[] result = {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE};
}
else{
BigInteger[] partial = matrixPow(matrix, n/2);
BigInteger[] result = matrixMultiply(partial, partial);
if(n%2 == 1){
result = matrixMultiply(result,matrix);
}
return result;
}
}
是我到目前为止的代码,但是编译器说它并不总是返回,但是在else语句中我必须更改什么
您应该将return result
上移一个范围。
private static BigInteger[] matrixPow(BigInteger[] matrix, int n){
BigInteger[] result;
if(n==0) {
result = {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE};
} else {
BigInteger[] partial = matrixPow(matrix, n/2);
result = matrixMultiply(partial, partial);
if(n%2 == 1) {
result = matrixMultiply(result,matrix);
}
}
return result;
}
这里的根本问题是格式设置(即缩进)不可用,这使得作用域的开始和结束位置很难看清。
返回值封装在else
块中。
在if else
块的if
部分中没有返回值,因此,如果到达if
块,它将不会返回任何内容,因此会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.