Kotlin when

텅텅텅·2023년 5월 28일
0

Kotlin

목록 보기
4/6

Kotlin provides a construct, "when" that is similar to the "switch" statement in Java.


fun main(){
	whatNumb(10)
}

fun whatNumb(numb:Int){
	when(numb){
		1 -> println("numb = 1")
        5 -> println("numb = 5")
        10 -> println("numb = 10")
    }
}

input value can be integers, decimals, Strings but logic

fun main(){
	whatNumb("choice2")
}

fun whatNumb(numb:Int){
	when(numb){
		choice1 -> println("Korean")
        choice2 -> println("English")
        choice3 -> println("Chinese")
    }
}

A conditional statement can be a range of numbers

fun main(){
	whereNumb(6)
}

fun whatNumb(numb:Int){
    when(numb){
        in 0..2 -> println("0 ~ 2 사이")
        in 3..5 -> println("3 ~ 5 사이")
        else -> println("6이상")
    }
}

profile
아무것도모르오

0개의 댓글