1.变量、常量
var(variable) 变量,可以无限次赋值,生命周期仅限于函数内部
var name = "Madrigal"
val(read only variable )只读变量,且只能赋值一次,不能修改
val isBlessed = true
const val 编译时常量,全局变量,只能赋值一次,必须在函数外初始化
const val MAX_EXP : Int = 5000
2.if...else...
可以用变量接收值
boolean isBig = if(9 > 8){
//do something
} else{
//do somethingelse
}
3.when
非常简洁的语法,还支持range类型的表达式(in downto until)
when (healthPoints) {
100 -> " is in excellent condition!"
in 90..99 -> " has a few scratches."
in 75..89 -> if (isBlessed) {
" has some minor wounds but is healing quite quickly!"
} else {
" has some minor wounds."
}
in 15..74 -> " looks pretty hurt."
else -> " is in awful condition!"
4.for...in... 更简单的循环