1. if, else, elseif
fun main(args: Array<String>) {
val name = "마드리길"
var healthPoints = 89
val isBlessed = true
val isImmortal = false
// 아우라
// if(isBlessed && healthPoints > 50 || isImmortal) {
val auraVisible = isBlessed && healthPoints > 50 || isImmortal
if(auraVisible) {
println("GREEN")
} else {
println("NONE")
}
}
2. 조건문 할당
fun main(args: Array<String>) {
val name = "마드리길"
var healthPoints = 89
val isBlessed = true
val isImmortal = false
val auraVisible = isBlessed && healthPoints > 50 || isImmortal
val auraColor = if(auraVisible) "GREEN" else "NONE"
println(auraColor)
val healthStatus = if(healthPoints == 100) "최상의 상태임!"
else if (healthPoints in 90..99) "약간의 찰과상만 있음."
else if (healthPoints in 75..89)
if(isBlessed) "경미한 상처가 있지만 빨리 치유됨!"
else "경미한 상처만 있음."
else if (healthPoints in 15..74) "많이 다친 것 같음"
else "최악의 상태임!"
println(name + " " + healthStatus)
}
- in 00..100
fun main(args: Array<String>) {
val name = "마드리길"
var healthPoints = 89
val isBlessed = true
val isImmortal = false
val auraVisible = isBlessed && healthPoints > 50 || isImmortal
val auraColor = if(auraVisible) "GREEN" else "NONE"
println(auraColor)
val healthStatus = if(healthPoints == 100) "최상의 상태임!"
else if (healthPoints in 90..99) "약간의 찰과상만 있음."
else if (healthPoints in 75..89)
if(isBlessed) "경미한 상처가 있지만 빨리 치유됨!"
else "경미한 상처만 있음."
else if (healthPoints in 15..74) "많이 다친 것 같음"
else "최악의 상태임!"
println(name + " " + healthStatus)
}
4. when, ->
fun main(args: Array<String>) {
val name = "마드리길"
var healthPoints = 89
val isBlessed = true
val isImmortal = false
val auraVisible = isBlessed && healthPoints > 50 || isImmortal
val auraColor = if(auraVisible) "GREEN" else "NONE"
println(auraColor)
val healthStatus = when(healthPoints) {
100 -> "최상의 상태임!"
in 90..99 -> "약간의 찰과상만 있음."
in 75..89 -> if (isBlessed) "경미한 상처가 있지만 빨리 치유됨!"
else "경미한 상처만 있음."
in 15..74 -> "많이 다친 것 같음"
else -> "최악의 상태임!"
}
println(name + " " + healthStatus)
}
5. 템플릿 문자열
fun main(args: Array<String>) {
val name = "마드리길"
var healthPoints = 89
val isBlessed = true
val isImmortal = false
val auraVisible = isBlessed && healthPoints > 50 || isImmortal
val auraColor = if(auraVisible) "GREEN" else "NONE"
val healthStatus = when(healthPoints) {
100 -> "최상의 상태임!"
in 90..99 -> "약간의 찰과상만 있음."
in 75..89 -> if (isBlessed) "경미한 상처가 있지만 빨리 치유됨!"
else "경미한 상처만 있음."
in 15..74 -> "많이 다친 것 같음"
else -> "최악의 상태임!"
}
println("(Aura: $auraColor)" +
"(Blessed: ${if (isBlessed) "YES" else "NO"})")
println("$name $healthStatus")
}