DEV Community

sunj
sunj

Posted on

Kotlin when, 2024-03-17

fun example(score : Int){
   when(){
      0 -> println("this is 0")
      1 -> println("this is 1")
      2,3 -> println("this is 2 or 3")
      else -> println("I dont't know")
   }
}
Enter fullscreen mode Exit fullscreen mode

2,3으로 지정할 수 있다

var b : Int = when(score){
   1-> 1
   2-> 2
   else -> 3
}
Enter fullscreen mode Exit fullscreen mode
   when(){
      in 90..100 -> println("You are genius")
      in 10..80 -> println("not bad")
      else -> println("okay")
   }
Enter fullscreen mode Exit fullscreen mode

else를 꼭 써줘야함

참조 : 유투브 강의 Code with Joyce

Top comments (0)