标签:多层 一个 list under 微软 table aci cal ==
1.if else
/** * if else */ val age =18 if (age < 18 ){ println("no allow") }else if (18<=age&&age<=20){ println("allow with other") }else{ println("allow self") } |
2. for ,while,do…while
/** * to和until * 例: * 1 to 10 返回1到10的Range数组,包含10 * 1 until 10 返回1到10 Range数组 ,不包含10 */
println(1 to 10 )//打印 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 println(1.to(10))//与上面等价,打印 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
println(1 to (10 ,2))//步长为2,从1开始打印 ,1,3,5,7,9 println(1.to(10, 2))
println(1 until 10 ) //不包含最后一个数,打印 1,2,3,4,5,6,7,8,9 println(1.until(10))//与上面等价
println(1 until (10 ,3 ))//步长为2,从1开始打印,打印1,4,7
|
/** * for 循环 * */ for( i <- 1 to 10 ){ println(i) } |
//可以分号隔开,写入多个list赋值的变量,构成多层for循环 //scala中 不能写count++ count-- 只能写count+ var count = 0; for(i <- 1 to 10; j <- 1 until 10){ println("i="+ i +", j="+j) count += 1 } println(count);
//例子: 打印小九九 for(i <- 1 until 10 ;j <- 1 until 10){ if(i>=j){ print(i +" * " + j + " = "+ i*j+" ")
} if(i==j ){ println() }
} |
//可以在for循环中加入条件判断 for(i<- 1 to 10 ;if (i%2) == 0 ;if (i == 4) ){ println(i) } |
//将for中的符合条件的元素通过yield关键字返回成一个集合 val list = for(i <- 1 to 10 ; if(i > 5 )) yield i for( w <- list ){ println(w) }
/** * while 循环 */ var index = 0 while(index < 100 ){ println("第"+index+"次while 循环") index += 1 } index = 0 do{ index +=1 println("第"+index+"次do while 循环") }while(index <100 ) |
标签:多层 一个 list under 微软 table aci cal ==
原文地址:https://www.cnblogs.com/xuange1/p/12253565.html