一.定义函数
def max(x: Int, y: Int): Int = {
val t = if(x > y) x else y
t
}
def say: Unit = {
printf("hahahaha~~~")
}
def main(args: Array[String]): Unit = {
val rst = max(1, 100)
println(s"${rst}")
say
}
改动使3个数最大值,求三个数的较大者
def max(x: Int, y: Int): Int = {
val t = if(x > y) x else y
t
}
def main(args: Array[String]): Unit = {
val rst = max(1, 10, 100)
println(s"${rst}")
}
三选一, 两次 二选一
def max(x:Int, y:Int, z:Int):Int = {
var t = if(x>y) x else y
if(t>z) t else z
}
def main(args: Array[String]): Unit = {
val rst = max(1000,200,100)
println(s"${rst}")
}
求三个数的最大值和最小值,函数需要返回多个值。把要放回的数据使用()包在一起,成为一个整体
def max(x:Int, y:Int, z:Int): (Int, Int) = {
var maxValue = if(x>y) x else y
maxValue = if(maxValue>z) maxValue else z
var minValue = if(x>y) y else x
minValue = if(minValue>z) z else minValue
(minValue, maxValue)
}
def main(args: Array[String]): Unit = {
val rst = max(1000,200,100)
println(s"最小值是:${rst._1},最大值是:${rst._2}")
}