且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在这里处理堆栈溢出异常

更新时间:2023-08-22 15:43:34

肯定是一项功课!

这是一种递归方法,可能正在对自然数求和.以下应该没问题:
Surely a homework!

It''s a recursive method that probably is doing sum of natural numbers. Following should be ok:
public int sum(int n)
{
     if(n==1)
        return 1;

     int s;
     s = n + sum(n-1);
     return s ;
}


最常见的堆栈溢出原因是深度过大或无限递归,因此您需要以某种条件终止.
The most common cause of stack overflow is excessively deep or infinite recursion.So you need to terminate with some condition.