在Kotlin 中并没有switch 操作符 , 取而代之的是when
java的写法:
int i= 5;
switch (i){
case 5:
System.out.print("5");
break;
case 4:
System.out.print("4");
break;
case 3:
System.out.print("3");
break;
case 2:
System.out.print("2");
break;
case 1:
System.out.print("1");
break;
}
Kotlin 写法:
val i = 5
when (i) {
5 -> print("5")
4 -> print("4")
3 -> print("3")
2 -> print("2")
1 -> print("1")
}
网友评论