import java.util.*;
public class NRootx {
public static Double NRmethod (Double num, int root, Double guess){
guess= num/(root+1); //first guess
Double guess_output=1.0;
for (int i=1;i<=root; i++)
{
guess_output *= guess;
}
Double error= guess_output-num;
error = error * 0.001;
guess = guess - error;
if (Math.abs(error)>=0.000001)
return NRmethod(num, root, guess); //Recursion
else
return guess;
//I can't find out the problem now. Java.lang.StackOverFlow error is showing.
}
}
}
// Main function
public static void main(String[] args) {
Double x;
int n;
Scanner sc= new Scanner (System.in);
System.out.println("Enter the value of x: ");
x=sc.nextDouble();
System.out.println("Enter the value of n: ");
n=sc.nextInt();
Double guess=x/(n+1);
Double ans= NRmethod(x,n,guess);
System.out.println("The value of y is ="+ans);
}
私はこれを解決するのを手伝ってください。私はこれを何度もやっていることに疲れている。エラーはありません! java.lang.StackOverflowErrorで解決策を見つけることができません
'StackOverflowError'とは関係がありませんが、' Double'の代わりに 'double'を使うので、不要な' Double'オブジェクトを作る必要はありません。 –
また、 'guess_output = Math.pow(推測、ルート);'、forループの必要はありません。 –