Kotlin中的三元运算符 [英] Ternary operator in kotlin

查看:224
本文介绍了Kotlin中的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用Java编写

int i = 10;
String s = i==10 ? "Ten" : "Empty";

即使我可以在方法参数中传递它.

Even I can pass it in method parameter.

callSomeMethod(i==10 ? "Ten" : "Empty");

如何将其转换为kotlin?在Kotlin中编写相同内容时,Lint显示错误.

How do I convert it to kotlin? Lint shows error when writing same thing in kotlin.

推荐答案

callSomeMethod( if (i==10) "Ten" else "Empty")

关于三元运算符的讨论: https://discuss.kotlinlang.org/t/ternary-operator/2116/3

Discussion about ternary operator: https://discuss.kotlinlang.org/t/ternary-operator/2116/3

这篇关于Kotlin中的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆