标签:scala
Scala while 循环
和许多语言类似,while 循环在条件为真的时候会持续执行一段代码块。例如,下面的代码在下一个星期五,同时又是13号之前,每天打印一句抱怨的话:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//
code-examples/Rounding/while-script.scala //
WARNING: This script runs for a LOOOONG time! import
java.util.Calendar def
isFridayThirteen(cal:
Calendar):
Boolean =
{ val
dayOfWeek =
cal.get(Calendar.DAY_OF_WEEK)
val
dayOfMonth =
cal.get(Calendar.DAY_OF_MONTH)
//
Scala returns the result of the last expression in a method (dayOfWeek
==
Calendar.FRIDAY) && (dayOfMonth ==
13)
}
while
(!isFridayThirteen(Calendar.getInstance())) { println("Today
isn‘t Friday the 13th. Lame.")
//
sleep for a day Thread.sleep(86400000)
} |
你可以在下面找到一张表,它列举了所有在while 循环中工作的条件操作符。
Scala do-while 循环
和上面的while 循环类似,一个do-while 循环当条件表达式为真时持续执行一些代码。唯一的区别是do-while 循环在运行代码块之后才进行条件检查。要从1 数到10,我们可以这样写:
|
1
2
3
4
5
6
|
//
code-examples/Rounding/do-while-script.scala var
count =
0;do
{ count
+=
1 println(count)
}while
(count < 10); |
标签:scala
原文地址:http://blog.csdn.net/crxy2016/article/details/44920437