Scala流程控制
3.2For循环控制
3.2.5 for嵌套循环
使用Scala for循环嵌套 实现九九乘法表: 常规手段: 见Scala-Day3
Scala手段:
3.2.6 引入变量
1)基本语法 2)实例:
for(i<-1 to 4){
val j = 4 -i
println("i= "+i+", j="+j)
}
println("--------------------------------------------------------------")
for(i<-1 to 3;j=3-i){
println("i= "+i+", j="+j)
}
println("--------------------------------------------------------------")
for {
i<-1 to 5
j=5-i
}{
println("i= "+i+", j="+j)
}
打印金字塔(九行):
分析:
代码实现:
package three
object Test03_Pyramid {
def main(args: Array[String]): Unit = {
for (i<-1 to 9){
var stars=2*i-1
val spaces=9-i
println(" "*spaces+"*"*stars)
}
println("-------------------------------------------------------")
for(i<-1 to 9;stars = 2*i-1;spaces=9-i){
println(" "*spaces+"*"*stars)
}
}
}
好了,十点半了,明天要早起,一日之气在于晨,早起永远的神hh
每日一句: There is no chance,no destiny,no fate,that can hinder or control the firm resolve of a determined soul. 一颗坚定的心不会被任何的命运所阻挠。 ----------------------------------------------------------------------------------- Ella Wheeler Wilcox
|