这是来自过去试卷的问题。有5个问题。三是错误,将阻止代码正确运行,另外两个在声明和Javadoc中。Java - 某些代码出错
我不相信我的答案,但我认为:
1.方法声明不应该是无效的,你应该要为这两种方法调用返回的双重价值。你也可以在方法声明中加双。然后
2.You需要添加一个return语句
3.添加到存储值的方法的局部变量。
4.Possibly那么你就需要@return
在Javadoc如果我添加一个return语句
5.Not知道这个错误
/**
* Calculate an expression a*x^n.
* @param a the multiplying factor
* @param x the number to be raised to a power
* @param n the power
*/
//original code
public void aPower(double a, double x, double n) {
if (n % 2 == 0) {
aPower(a, x * x, n/2);
} else {
aPower(a * x, x * x, n/2);
}
}
/**
* Calculate an expression a*x^n.
* @param a the multiplying factor
* @param x the number to be raised to a power
* @param n the power
* @return the answer to the expression
*/
//modified code
public double aPower(double a, double x, double n) {
double temp = 1.0;
if (n % 2 == 0) {
temp = aPower(a, x * x, n/2);
} else {
temp = aPower(a * x, x * x, n/2);
}
return temp;
}
你不需要临时变量;你可以直接返回。 – Joey
我不明白你的意图。在你修改后的代码中,如果n是偶数,你将覆盖'temp'。有些事情似乎并不正确。 – Makoto
你有什么错误? – SiB