如果我下面的代码不在 for() 循环中,当我的“n2”值超过 7853 时,我很难理解为什么会出现 StackOverflowError。
这是代码:
package tp3;
import java.util.Date;
public class TP3 {
//Somme des fractions Récursive
public static double somFractionsRecursive(double index) throws IllegalArgumentException{
if (index == 0) throw new IllegalArgumentException("Erreur: impossible de diviser par zéro");
if (index == 1) return 1/index;
double reponse = (1/index) + somFractionsRecursive(index-1);
return reponse;
}
//Somme des fractions itérative
public static double somFractionsIterative(double index) throws IllegalArgumentException{
if (index == 0) throw new IllegalArgumentException("Erreur: impossible de diviser par zéro");
double reponse = 0;
for (double i = 1; i <= index; i++) {
reponse += 1/i ;
}
return reponse;
}
public static void main(String[] args) {
Date d = new Date();
double n1 = 999, n2 = 7853;
System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en récursivité: ");
d = new Date();
long debut = d.getTime();
System.out.println("somFractionsRecursive("+n1+") = " + somFractionsRecursive(n1));
d = new Date();
long fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en récursivité: ");
d = new Date();
debut = d.getTime();
System.out.println("somFractionsRecursive("+n2+") = " + somFractionsRecursive(n2));
d = new Date();
fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en itération: ");
d = new Date();
debut = d.getTime();
System.out.println("somFractionsIterative("+n1+") = " + somFractionsIterative(n1));
d = new Date();
fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en itération: ");
d = new Date();
debut = d.getTime();
System.out.println("somFractionsIterative("+n2+") = " + somFractionsIterative(n2));
d = new Date();
fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
}// End Main
}// End TP3 Class
但是,我将最后一段代码放在 for() 循环中,我可以毫无问题地获得高达 9999 的“n2”变量
以下代码没有给我 StackOverflowError:
// Functions are the same as above...
System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en itération: ");
d = new Date();
debut = d.getTime();
for (int i = 1; i <= n1; i++) {
System.out.println("somFractionsIterative("+i+") = " + somFractionsIterative(i));
}
d = new Date();
fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en itération: ");
d = new Date();
debut = d.getTime();
for (int i = 1; i <= n2; i++) {
System.out.println("somFractionsIterative("+i+") = " + somFractionsIterative(i));
}
d = new Date();
fin = d.getTime();
System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");
对不起,我对此很陌生!并一如既往地感谢您的帮助:)