Scala 函数嵌套

返回上一级

函数嵌套 是指在函数内再定义函数,定义在函数内的函数称之为局部函数。

下面我们用 函数嵌套 方式来实现阶乘

object Test {
   def main(args: Array[String]) {
      println( factorial(1) )
      println( factorial(2) )
      println( factorial(3) )
      println( factorial(4) )
      println( factorial(5) )
   }

   def factorial(i: Int): Int = {
      def fact(i: Int, accumulator: Int): Int = {
         if (i <= 1)
            accumulator
         else
            fact(i - 1, i * accumulator)
      }
      fact(i, 1)
   }
}

上面代码执行结果为:

1
2
6
24
120

返回上一级

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.