欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Kotlin 中 switch 写法

发布时间:2023/11/27 72 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Kotlin 中 switch 写法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在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 = 5when (i) {5 -> print("5")4 -> print("4")3 -> print("3")2 -> print("2")1 -> print("1")}

 

总结

以上是生活随笔为你收集整理的Kotlin 中 switch 写法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。