Java中三元运算符中的单个表达式
我知道你可以拥有
String answer = (5 == 5) ? "yes" : "no";
是否可以以某种方式仅拥有:
String answer = (5 == 5) ? "yes";
当我尝试时它会给出编译错误。
注意:(5==5)
只是一个示例。取而代之的是可能是真也可能是假的陈述。
I know you can have
String answer = (5 == 5) ? "yes" : "no";
Is it somehow possible to have only:
String answer = (5 == 5) ? "yes";
It gives a compile error when I try.
NOTE: (5==5)
is just an example. In its place will be statement which could be either true or false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果一行很重要
因为字符串的默认值为空。
if one line is important
Since a String's default value is null.
您正在寻找
if
语句:您的想法是不可能的,因为表达式(例如条件值)必须始终有一个值。
在您的代码中,如果
5 != 5
,则表达式将没有值,这没有任何意义。You're looking for an
if
statement:Your idea is impossible because an expression (such as the conditional value) must always have a value.
In your code, if
5 != 5
, the expression would have no value, which wouldn't make any sense.不,你不能拥有它。您需要同时指定 ?和 :。
使用直接的 if。
No. you can't have it. You need to specify both the ? and :.
Use a straight if.