if(x>0) 1 else -1
val s = if(x>0) 1 else -1
if(x>0) 1 else -1
的类型是Int,因为两个分支的类型都是Int。对于混合类型表达式,分支的类型不一样,得到的类型是两个分支类型的超类型
.val s = if(x>0) 1 else "None"
// s: Any = 1
val s = if(x>0) 1 else -1.0
// s: Double = 1.0
//如果只有一个分支
val s = if(x>0) 1
//s: AnyVal = 1
val s = if(x>0) "a"
//s: Any = a
val s:String = if(x>0) "add"
//:12: error: type mismatch;
// found : Unit
// required: String
// val s:String = if(x>0) "add"
// ^
val s:String = if(x>0) "add" else "dfd"
// s: String = add
( )
,括号里可以加空格,也可以不加val a:Unit=( )
// a: Unit = ()
a.getClass
// Class[Unit] = void
:paste
回车,输入内容,结束后按下Ctrl+D
组合键结束键盘输入。()
。 val k={3>2}
// k: Boolean = true
val k={var a=1;var b=2}
// k: Unit = ()
val y=x=1
// y: Unit = ()
x
//Int = 1
val a=b=c=1 //错误
// :11: error: not found: value b
// val a=b=c=1
printf("Hello %s haha %d,%n","world",100)
// Hello world haha 100,
val name = "Sam";val age =12
print(f"Hello,$name! In six months, you will be ${age + 0.5}%7.2f years old.%n")
//Hello,Sam! In six months, you will be 12.50 years old.
print(s"Hello,${"Bargins"}! In six months, you will be ${var age =93;age+0.5} years old.")
//Hello,Bargins! In six months, you will be 93.5 years old.
readBoolean、 readByte 、 readChar 、 readDouble 、 readFloat 、 readInt readLine 、 readLong 、 readShort
for (i <- 表达式) {做些什么}
1 to 10
,如果不包含后面的可以用 1 until 10
字符串for(i<-1 to 10){print(i)}
// 12345678910
val s="World"
for(i<-0 until s.length){print(s.charAt(i)+" , ")}
// W , o , r , l , d ,
for(i <- s){print(i+" , ")}
// W , o , r , l , d ,
首先导入 import scala.util.control.Breaks._
break的功能
val array = Array(1,3,10,5,4)
breakable{
for (i<- array){
if (i>5) {break}
println(i)
println("haha")
}
}
实现continue的功能
val array = Array(1,3,10,5,4)
for (i <- array){
breakable {
if (i > 5) break
println(i)
}
}
变量<-表达式
for(i<-1 to 3;j<-1 to 3 ){print(f"${10*i+j}%3d")}
// 11 12 13 21 22 23 31 32 33
for(i<-1 to 3;j<-1 to 3 if i!=j ){print(f"${10*i+j}%3d")}
// 12 13 21 23 31 32
for(i<-1 to 3;j<-1 to 3 if i!=j ) yield 10*i+j
// scala.collection.immutable.IndexedSeq[Int] = Vector(12, 13, 21, 23, 31, 32)
//
var sum=0
for(i<-1 to 3;j<-1 to 3 if i!=j ) yield {sum+=10*i+j;sum}
// scala.collection.immutable.IndexedSeq[Int] = Vector(12, 25, 46, 69, 100, 132)
def
定义的是方法,而函数是通过=>
定义。这里不做严格的概念区分了,def的也算函数吧。//方法
def abs(x:Double):Double=if(x>=0) x else -x
// abs: (x: Double)Double
// 函数
val absf=(x:Double)=>if(x>=0) x else -x
// absf: Double => Double =
val 函数名:( 输入参数类型 => 输出参数类型) = {(输入参数) => 函数体}
( 输入参数类型 => 输出参数类型)
,函数的值为{(输入参数) => 函数体}
val sum:((Double,Double)=>Double)={(x,y)=>{x+y}}
// sum: (Double, Double) => Double =
//省略返回类型
val sum1=(x:Double,y:Double)=>{x+y}
// sum1: (Double, Double) => Double =
//继续化简,使用下划线
val sum2=(_:Double)+(_:Double)
sum2: (Double, Double) => Double = <function2>
//Triple
val triple=(_:Double)*3
// triple: Double => Double =
//常用于高阶函数中,函数作为参数
Array(3,2,5).map(_*3)
// Array[Int] = Array(9, 6, 15)
def add(a:Int,b:Int,c:Int)={s"$a,$b,$c"}
// add: (a: Int, b: Int, c: Int)String
add(2,1,4)
// String = 2,1,4
add(c=2,a=1,b=4)
// String = 1,4,2
def add2(a:Int,b:Int=10,c:Int=100)={s"$a,$b,$c"}
// (a: Int, b: Int, c: Int)String
add2(3)
// String = 3,10,100
add2(3,20)
// String = 3,20,100
add2(3,c=5,b=4)
//String = 3,4,5
参数名:单个参数类型*
def sum(args:Int*)={
var result=0
for(arg<-args) result+=arg
result
}
//
sum(2,3,4)
// Int = 9
sum(2,3,4,5,6)
// Int = 20
变量名、冒号、空格、下划线、星号
val arr=Array(2,3,4,5,6)
// arr: Array[Int] = Array(2, 3, 4, 5, 6)
sum(arr: _*)
// Int = 20
//参数中可以加入多个空格
sum(arr : _ *)
// Int = 20
// 其他序列
val arr2 = 1 to 10
// scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sum(arr2: _*)
// Int = 55
scala> def sum1(a:Int,b:Int)={a+b}
sum1: (a: Int, b: Int)Int
scala> def sum2(a:Int,b:Int){a+b}
sum2: (a: Int, b: Int)Unit
scala> def sum3(a:Int,b:Int){print(a+b)}
sum3: (a: Int, b: Int)Unit
// 调用
scala> sum1(1,2)
res1: Int = 3
scala> sum2(1,2)
scala> sum3(1,2)
3
def main(args: Array[String]) {}
//或
def main(args: Array[String]):Unit={}
// 不声明为lazy,如果文件不存在会直接报错
scala> val words=scala.io.Source.fromFile("C:")
java.io.FileNotFoundException: C: (拒绝访问。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at scala.io.Source$.fromFile(Source.scala:91)
at scala.io.Source$.fromFile(Source.scala:76)
at scala.io.Source$.fromFile(Source.scala:54)
... 32 elided
//声明为lazy,有错误也不会报
scala> lazy val words=scala.io.Source.fromFile("C:")
words: scala.io.BufferedSource = <lazy>
//使用时报错
scala> words.mkString
java.io.FileNotFoundException: C: (拒绝访问。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at scala.io.Source$.fromFile(Source.scala:91)
at scala.io.Source$.fromFile(Source.scala:76)
at scala.io.Source$.fromFile(Source.scala:54)
at .words$lzycompute(<console>:11)
at .words(<console>:11)
... 32 elided
throw new Exception("Some Errors")
// java.lang.Exception: Some Errors
// ... 32 elided
Nothing
,在if/else语句中,如果一个分支的类型是Nothing,那么语句的类型是另一个分支的类型。(好像没用) for(i<- -3 to 3) {
try {
println(f"1 ÷ $i = ${1 / i}%.2f")
} catch {
case _: Exception => println("hah")
}
}
// 1 ÷ -3 = 0.00
// 1 ÷ -2 = 0.00
// 1 ÷ -1 = -1.00
// hah
// 1 ÷ 1 = 1.00
// 1 ÷ 2 = 0.00
// 1 ÷ 3 = 0.00