Java 牛顿平方根法
我们有一个叫做牛顿的课程,我们使用牛顿的平方根法。这是我想出的代码。我们必须使用递归来找到答案。这是我们用来找到答案的公式, Xn=(Xn-1-(Xn-1)^2-k)/2(Xn1)。Xn=新的猜测或变量t。Xn-1=用户猜测或g。K=被平方的数字。此方法将一直执行,直到新的猜测t等于答案。所以我的问题是这看起来对吗Java 牛顿平方根法,java,Java,我们有一个叫做牛顿的课程,我们使用牛顿的平方根法。这是我想出的代码。我们必须使用递归来找到答案。这是我们用来找到答案的公式, Xn=(Xn-1-(Xn-1)^2-k)/2(Xn1)。Xn=新的猜测或变量t。Xn-1=用户猜测或g。K=被平方的数字。此方法将一直执行,直到新的猜测t等于答案。所以我的问题是这看起来对吗 class Newton { public static void main(String[] args) { double k = 25; // number that
class Newton {
public static void main(String[] args) {
double k = 25; // number that is being squarted
double g = 10; // number we are quessing
double x = 0; // x holds the answers
x = Math.sqrt(k);
System.out.print(x);
int answer= find(t);
System.out.println("The square root is " + answer);
}
public static int find(double k, double g, double x, double t){
if (g == x){
} return g;
else{
t = (x - 1) - (Math.pow(x - 1, 2) - k) / (2 * (x - 1));
g = t;
} return (int) t;
}
}尝试更正返回语句,如下所示:
public static int find(double k, double g, double x, double t){
if (g == x){
return (int) g;
}
else{
t = (x - 1) - (Math.pow(x - 1, 2) - k) / (2 * (x - 1));
g = t;
return (int) t;
}
}
你有
int
return notdouble
,但有地方错了。我看到了语句和代码,但没有一个简单易懂的问题。请通过直接向我们提问来解决此问题,以帮助您回答。还要告诉我们:这段代码在做什么,你不希望它做什么?什么是你不想让它做的?详细信息将非常有用。您甚至没有调用find方法,所以我不知道它如何工作。然后通过值传递双参数,而不是引用。这不是代码审阅站点。