Sonny
Posts: 9
Nickname: proban
Registered: Dec, 2002
|
|
Re: Recursive method
|
Posted: Dec 10, 2002 12:01 AM
|
|
Hi Marie Harris, This looks like a homework. Anyway, the problem in your recursive method is that it never changes the value of n in the later call. So, it will loop forever until stack overflowed --> error. Look like the method used to sum all numbers from n to 0. So, it can be correct like:
public static int sum(int n ) { if ( n == 0 ) return 0; else if (n < 0) return n + sum(++n); else return n + sum(--n); }
Good luck.
Sonny
|
|