【发布时间】:2016-03-16 11:32:25
【问题描述】:
我在运行程序时遇到 stackoverflow 错误。我对java非常了解,我可以使用一些建议。
感谢任何帮助!
public class ApproxSquareRoot {
public static float sqrRootRec(float number, float approx, float tol) {
if((Math.abs((Math.pow(approx,2) - number))<= tol*number)) {
return approx;
} else
return sqrRootRec(number, (approx*approx + number/(2*approx)),tol);
}
public static float sqrRoot(float number, float approx, float tol) {
while (Math.abs((Math.pow(approx,2) - number))<= tol*number)
approx = (approx*approx + number)/(approx + approx);
return approx;
}
}
.
Input number: 43
Input approx: 2.5
Input tol: .1
Output with number = 43.0 approx = 2.5 tolerance = 0.1
Exception in thread "main" java.lang.StackOverflowError
【问题讨论】:
-
你研究过
StackOverflowError是什么吗? -
你可能得到了一个无限递归。
-
@TimBiegeleisen 我该如何解决这个问题.. 你能帮忙吗?
-
检查下面的答案,因为它似乎在正确的轨道上。