标签:基本数据类型 val readline 包装类 child 整数 操作符 double OLE
for(i <- 1 to 9;j <- 1 to 9){
if(j==9){
println(i*j)
}else{
print(i*j+" ")
}
}
def sayHello(name:String,age: Int) = {
if(age > 19) {
printf("hi %s,you are a big boy\n",name)
age
}else{
printf("hi ,%s,you are a children\n",name)
age
}
}
def sum(n:Int) = {
var result = 0
for( i<- 1 to n){
result += i
}
result
}
def fab(n: Int): Int = {
if(n <= 0) 1
else fab(n-1)+fab(n-2)
}
def sayHello(firstName:String,middleName:String = "",lastName:String="") = firstName + " " +middleName+" "+lastName
sayHello(firstName="hahha",middleName="xxx",lastName="ggg")
def sum(nums: Int*)={
var result = 0
for(num <- nums){
result += num
}
result
}
sum(1 to 5: _*) //值为15,表示取出1到5的整数相加
def sum2(nums:Int*):Int = {
if(nums.length == 0) 0
else nums.head + sum2(nums.tail: _*)
}
//定义过程,不会有返回值
def sayHello(name:String):Unit = "Hello,"+name
lazy val lines = fromFile("/home/1.text").mkString
//使用的时候才会执行上面这句
print(lines)
异常
try{
throw new lllegAlrgumentException("x should not be negative")
}catch{
case_:lllegAlrgumentException => print("sorry,error")
}finally{
print("release io ")
}
标签:基本数据类型 val readline 包装类 child 整数 操作符 double OLE
原文地址:https://www.cnblogs.com/sky-chen/p/10061307.html