apply方法也是函数的入口
scala> val f = (x: Int) => x+1
f: Int => Int = $$Lambda$1180/381936788@4b682e71
scala> f.apply(3)
res9: Int = 4
scala> f(5)
res10: Int = 6
----------------------------------
scala读取输入 可以使用readLine()
scala> readLine()
<console>:12: warning: method readLine in trait DeprecatedPredef is deprecated (since 2.11.0): use the method in `scala.io.StdIn`
readLine()
^
res0: String = jhello
----------------------------------
提示 readLine() 已经过时 ,可以使用新的 StdIn.readLine()
scala> import scala.io.StdIn
import scala.io.StdIn
scala> StdIn.readLine()
res2: String = helow heloo'
可以把读入赋给一个变量
scala> val name = StdIn.readLine("plean put your name:")
plean put your name:name: String = catlinan
----------------------------------
while 循环:
scala> :paste
// Entering paste mode (ctrl-D to finish)
var n=10
while(n>0){
println(n)
n = n-1
}
// Exiting paste mode, now interpreting.
10
9
8
7
6
5
4
3
2
1
n: Int = 0
scala>
----------------------------------
for循环
scala> for(i <- 1 to 10){print(i)}
12345678910
----------------------------------
for的多重循环 打印99乘法表
scala> :paste
// Entering paste mode (ctrl-D to finish)
for(a<- 1 to 9; b<- 1 to 9){
print(a+"*"+b+"="+(a*b)+" ")
if(b==9) println()
}
// Exiting paste mode, now interpreting.
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 8*9=72
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
----------------------------------
可以for循环一个集合
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for 循环
for( a <- numList
if a != 3; if a < 8 ){
println( "Value of a: " + a );
}
// Exiting paste mode, now interpreting.
Value of a: 1
Value of a: 2
Value of a: 4
Value of a: 5
Value of a: 6
Value of a: 7
numList: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
----------------------------------
scala> for(c <- "hello world"){print(c+" ")}
h e l l o w o r l d
scala> :paste
// Entering paste mode (ctrl-D to finish)
----------------------------------
for 使用 yield
你可以将 for 循环的返回值作为一个变量存储
scala> :paste
// Entering paste mode (ctrl-D to finish)
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for 循环
var retVal = for{ a <- numList
if a != 3; if a < 8
}yield a
// 输出返回值
for( a <- retVal){
println( "Value of a: " + a );
}
// Exiting paste mode, now interpreting.
Value of a: 1
Value of a: 2
Value of a: 4
Value of a: 5
Value of a: 6
Value of a: 7
numList: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
retVal: List[Int] = List(1, 2, 4, 5, 6, 7)
yield后面是个表达式
scala> var xx=for( x<- 1 until 10 if x%2==0) yield x*2
xx: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 8, 12, 16)
----------------------------------
until 不包含右边界
scala> for(t <- 0 until 10){print(t)}
0123456789
----------------------------------
使用flag 跳出循环
scala> :paste
// Entering paste mode (ctrl-D to finish)
var flag=true;
for(x<- 0 to 10 if flag){
print(x)
if(x==5) flag=false
}
// Exiting paste mode, now interpreting.
012345flag: Boolean = false
----------------------------------
使用Breaks类的break方法
scala> import scala.util.control.Breaks._
import scala.util.control.Breaks._
scala> :paste
// Entering paste mode (ctrl-D to finish)
breakable{
for(x<- 0 to 10){
print(x)
if(x==5){ break}
}
}
// Exiting paste mode, now interpreting.
012345
----------------------------------
if 守卫
scala> for( x <- 0 until 20 if x%2==0){ print(x+" ")}
0 2 4 6 8 10 12 14 16 18
----------------------------------
for推导式
scala> for(x <- 1 to 10 if x%2==0) yield x
res22: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
scala> var newList = for( x <- 0 until 20 if x%2==0) yield x
newList: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
scala> print(newList)
Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
----------------------------------