基本介绍
Scala是运行在Java虚拟机上的(Java virtual machine),因此具有如下特点
- 轻松实现和Java类库互通
- 它既支持面向对象式编程,又支持函数式编程
- 它写出的程序像动态语言一样简洁,但事实上它确是严格意义上的静态语言。Scala就像一位武林中的集大成者,将过去几十年计算机语言发展历史中的精萃集于一身,化繁为简,为程序员们提供了一种新的选择。设计者马丁·奥得斯基希望程序员们将编程作为简洁,高效,令人愉快的工作。同时也让程序员们进行关于编程思想的新的思考。
递归的应用题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.sky.scala.exercise17
object CalculationRecursionTime { def main(args: Array[String]): Unit = { val currentMills = System.currentTimeMillis() var n1: BigInt = 1 var sum: BigInt = 0 while (n1 <= 99999999L) { n1 += 1 sum += n1 } println(sum) val currentMills2 = System.currentTimeMillis()
println("循环总用时" + (currentMills2 - currentMills) + "毫秒")
}
def recursionNum(n1: BigInt, sum: BigInt): BigInt = { if (n1 <= 99999999L) return recursionNum(n1 + 1, sum + n1) else return sum }
}
|
字符反转操作
1 2 3 4 5 6 7 8 9 10 11 12
| package com.sky.scala.exercise17.recursionDemo
object GetFactorial { def main(args: Array[String]): Unit = { println(getFactorial(3)) }
def getFactorial(n: Int): Int = { if (n == 0) 1 else n * getFactorial(n - 1) } }
|
求阶层
1 2 3 4 5 6 7 8 9 10 11 12
| package com.sky.scala.exercise17.recursionDemo
object GetFactorial { def main(args: Array[String]): Unit = { println(getFactorial(3)) }
def getFactorial(n: Int): Int = { if (n == 0) 1 else n * getFactorial(n - 1) } }
|