递归函数

private void GetValue(int num, ref int result)
        {
            if (num != 1)
            {
                result = result * num;
                GetValue(num - 1, ref result);
            }
        }

调用:

        int num = 5;
        int result = 1;//求阶乘时,result必须是1
        GetValue(num,ref result);
  结果:
  120

更多推荐