<>Scala流程控制
<>3.2For循环控制
<>3.2.5 for嵌套循环
使用Scala for循环嵌套 实现九九乘法表:
常规手段:
见Scala-Day3
Scala手段:
<>3.2.6 引入变量
1)基本语法
2)实例:
//6.循环引入变量(三种实例) 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