1.输入输出语句
1.1 输出语句:
scala> print("hello world")
hello world
scala> println("hello,world")
hello,world
格式化输出,%s代表String,%d代表Int:
scala> printf("hi,my name is %s,i'm %d years old","karol",32)
hi,my name is karol,i'm 32 years old
如果有变量需要输出,有两种形式的写法:
scala> val name = "joe"
name: String = joe
scala> println("my name is: " + name)
my name is: joe
scala> print(s"my name is: ${name}")
my name is: joe
1.2 输入语句:
readline():类似于java中的system.in或者scanner
scala> :paste
// Entering paste mode (ctrl-D to finish)
val name = readLine("Hello,what's your name?")
println(s"Hi,${name},what's the age?")
val age = readInt()
if(age > 18) {
printf("hi,%s,you're %d years old,come in!",name,age)
}else {
println(s"your age is ${age},not allowed!")
}
// Exiting paste mode, now interpreting.
Hello,what's your name?Hi,jack,what's the age?
hi,jack,you're 34 years old,come in!name: String = jack
age: Int = 34
2.条件控制语句
if...else,if...else if...else
if语句的返回值就是表达式的返回值,如下:
scala> val age = 30
age: Int = 30
这种表达就类似于java中的三元操作符
scala> if(age > 18) 1 else 0
res36: Int = 1
scala> var isAdult : Boolean = true
isAdult: Boolean = true
因为表达式是赋值语句,所以if语句的返回值为unit,即没有返回值
scala> if(age > 18) isAdult = true else isAdult = false
如果两个表达式的返回值类型不同,则取它们的公共父类
scala> if(age > 18) "adult" else 0
res41: Any = adult
也支持多重判断:
val score = 65
if (score > 90) {
println(s"your score is ${score}, level is 'A'")
} else if (score > 80 && score <= 90) {
println(s"your score is ${score}, level is 'B'")
} else {
println(s"your score is ${score}, level is 'C'")
}
结果:your score is 65, level is 'C'
3.循环语句
scala中没有break和continue
3.1 scala中产生集合的便捷方式:
产生1到10,步长为1的集合,相当于[1,10]
scala> 1 to 10
res45: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
产生1到10,步长为1的集合,但包左不包右[1,10)
scala> 1 until 10
res46: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
与1 until 10相同,[1,10)
scala> Range(1,10)
res47: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
产生1到10,步长为3的集合
scala> Range(1,10,3)
res48: scala.collection.immutable.Range = Range(1, 4, 7)
步长为负也可以,即为降序,以下这种写法会产生一个空集合
scala> Range(1,10,-3)
res49: scala.collection.immutable.Range = Range()
步长为负数的集合
scala> Range(10,1,-3)
res50: scala.collection.immutable.Range = Range(10, 7, 4)
这种写法其实相当于实现包右不包左,即(1,10]
scala> Range(10,1,-1)
res51: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2)
3.2 while循环:
一般的while循环
scala> var n = 10
n: Int = 10
scala> while (n > 0) {
| print(n + " ")
| n -= 1
| }
10 9 8 7 6 5 4 3 2 1
do while循环:
scala> n = 10
n: Int = 10
scala> do{
| print(n + " ")
| n -= 1
| }while (n > 0)
10 9 8 7 6 5 4 3 2 1
如果需要用到break,需要导包
scala> import scala.util.control._
import scala.util.control._
scala> val loop = new Breaks
loop: scala.util.control.Breaks = scala.util.control.Breaks@3841d54b
scala> loop.breakable( while (n > 0) {
| print(n + " ")
| if (n==5) {loop.break()}
| n -= 1
| })
10 9 8 7 6 5
如果需要用到continue,那只能通过别的办法实现
scala> while (n > 0) {
| if (n != 5) {
| print(n + " ")
| }
| n = n - 1
| }
10 9 8 7 6 4 3 2 1
3.3 for循环:
scala的for循环和java的for循环不太一样,scala的for循环使用'<-'符号来遍历集合,有点类似于java中的foreach
普通写法:
scala> for (i <- 1 to 10) {
| print(i + " ")
| }
1 2 3 4 5 6 7 8 9 10
scala> for (i <- "Hello,World!") {
| print(s"char: ${i} ")
| }
char: H char: e char: l char: l char: o char: , char: W char: o char: r char: l char: d char: !
for循环中加过滤:
普通写法:
scala> for (i <- 1 to 10) {
| if (i != 5) {print(i + " ")}
| }
1 2 3 4 6 7 8 9 10
if守卫写法(即把过滤写到for循环条件里)
scala> for (i <- 1 to 10 if i != 5) {
| print(i + " ")
| }
1 2 3 4 6 7 8 9 10
if守卫写法效率更高一些,因为过滤元素不需要进入循环体
双重for循环:
实现99乘法表
scala> for (i <- 1 to 9) {
| for(j <- 1 to i) {
| print(s"${i}*${j}=${i*j}\t")
| }
| println()
| }
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
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
也可以使用";"在两个区间进行循环,将迭代给定区间的所有可能值
scala> for (i <- 1 to 9 ; j <- 1 to i) {
| print(s"${i}*${j}=${i*j}\t")
| if(i == j) println()
| }
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
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
或者不用分号,改换行,这时需要用"{}"来包住条件
scala> for {i <- 1 to 9
| j <- 1 to i } {
| print(s"${i}*${j}=${i*j}\t")
| if (j == i) println()
| }
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
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循环绑定变量:
scala> val domains = Array("jianshu "," baidu "," ","google")
domains: Array[String] = Array("jianshu ", " baidu ", " ", google)
绑定变量实现去除前后空格
scala> for(i <- domains ; domain = i.trim()) {
| print(s"i:${i}*")
| print(s"domain:${domain}**")
| }
i:jianshu *domain:jianshu**i: baidu *domain:baidu**i: *domain:**i:google*domain:google**
yield关键字:
yield在scala中属于系统关键字,如果想要用java Thread中的yield方法,需要加"``"符号,如:
Thread.`yield`()
yield的作用就是将每一次循环的返回值,添加组合成一个新的集合
scala> val newStr = for (i <- "Hello world" if i != 'l') yield i
newStr: String = Heo word
网友评论