這種遞歸有什麼問題Java?此遞歸中的Stackoverflow錯誤
public class findPyt
{
public static int sum = 0;
public static void main(String[] args)
{
findP(3, 4, 5);
}
public static void findP(int a, int b, int c)
{
sum = a+b+c;
if (sum == 1000)
{
System.out.println("The Triplets are: "+ a +","+ b +","+ c);
}
else
{
findP(a*2, b*2, c*2);
}
}
}
我得到這個異常:
Exception in thread "main" java.lang.StackOverflowError
at hello.findP(hello.java:12)
at hello.findP(hello.java:19)
當我嘗試紅寶石做相同的,我得到這個:
SystemStackError: stack level too deep
def pythagoreanTriples(a=3, b=4, c=5)
if (a+b+c) == 1000
puts "The Triplets are: "+ a +","+ b +","+ c
else
pythagoreanTriples(a*2, b*2, c*2)
end
end
啊......錯過了那個基地的條件!謝謝!有效! – zengr 2010-09-19 22:50:36
更新了ruby代碼。 – zengr 2010-09-19 22:51:45