understanding basic recursion

后端 未结 8 2048
public static void main (String[] args)
{
    System.out.println(factorial(5));
}

public int factorial(int n)
{
    if(n <= 1){
        return 1;
    }
    else{         


        
8条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 16:25

    ....then 3 * (3-1)..... until it gets to 1 which will just return 1 right?

    right, but it returns that '1' to the next-to-last invocation, which will multiply by two, returning '2'... to the next-to-next-to-last, which will multiply by three.....

提交回复
热议问题