环境: CentOS 6.3
不解释,看例子,自己琢磨。
1. While 循环
$ cat while.scala
var i = 10;
while ( i > 0 ) {
println("this is number " + i);
i = i -1;
}
$ scala while.scala
this is number 10
this is number 9
this is number 8
this is number 7
this is number 6
this is number 5
this is number 4
this is number 3
this is number 2
this is number 1
2. foreach 循环
$ cat foreach.scala
var array=new Array[String](3) //创建数组并初始化
array(0) = "hello"
array(1) = "nihao"
array(2) = "ma?"
array.foreach(arr => println(arr)) // 用foreach 循环输出数组内容 arr是指array里面每个元素的参数,scala 会推断是string
//array.foreach((arr:String) => println(arr)) //也可以显示指定arr类型
3. for 循环
$ cat for.scala
for(arg<-args)
println(arg)
$ scala for.scala this is China
this
is
China
总结,函数式文本的语法:
时间: 2024-11-08 08:01:58