Maison  >  Questions et réponses  >  le corps du texte

java - 关于递归方法的问题

为什么最后会报错?

    public static int count(int n){
        int result;
        result = n/4+count(n-4);
        if (n<0)
            result = 0;
        return result;
    }
    
    public static void main(String[] args){
        System.out.println(count(5));
    }

Exception in thread "main" java.lang.StackOverflowError

at huam.count(huam.java:4)
at huam.count(huam.java:4)
at huam.count(huam.java:4)
at huam.count(huam.java:4)
黄舟黄舟2743 Il y a quelques jours405

répondre à tous(1)je répondrai

  • PHP中文网

    PHP中文网2017-04-18 10:56:36

    public static int count(int n){
            if (n<0)
                return 0;
            return n/4+count(n-4);
        }

    répondre
    0
  • Annulerrépondre