Scala 範例程式 - 總和 (sum)

原始程式: sum.scala

object sum {
  def main(args: Array[String]) {
    try {
      val elems = args map Integer.parseInt
      println("The sum of my arguments is: " + elems.foldRight(0) (_ + _))
    } catch {
      case e: NumberFormatException => 
        println("Usage: scala sum <n1> <n2> ... ")
    }
  }
}

執行結果:

D:\Scala\code>scalac sum.scala

D:\Scala\code>scala sum 3 7 2 1 9
The sum of my arguments is: 22
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License