浏览器中输入网址:play.kotlinlang.org 进去后黏贴代码点击Run就可以看到结果了。
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
import kotlin.math.max
fun main() {
println("Hello, world!!!")
//how to 声明一个变量,以及可变:use var和不可变:use val
//val a=10
var a=10
a=a*10
println("a= "+a)
//how to write a function 函数 fun is keyword fun XXX(xxx:type,xxxxx:type){}
val aa=37
val bb=40
val value=largeNumber(aa,bb)
println("larger number is "+value)
val value1=largeNumber1(aa,bb)
println("larger number use largeNumber1 is "+value1)
val value2=largeNumber2(aa,bb)
println("larger number use largeNumber2 is "+value2)
}
fun largeNumber(num1:Int,num2:Int):Int{
return max(num1,num2)//需要包的支持
}
//当函数体只有一行代码的时候,可以直接写个等于号,然后直接写这唯一的一行代码,甚至可以省略返回值的声明,如下面的Int
fun largeNumber1(num1: Int, num2: Int):Int = max(num1, num2)
fun largeNumber2(num1: Int, num2: Int) = max(num1, num2)//省略了返回值Int,kotlin可以推导出
运行结果:
Hello, world!!!
a= 100
larger number is 40
larger number use largeNumber1 is 40
larger number use largeNumber2 is 40