【发布时间】:2018-08-04 09:42:24
【问题描述】:
我有一段代码使用条件运算符将变量 x 的值与 5 进行比较:
(x >=5 ? x : -x)
与常规 if else 语句相比,使用条件运算符有什么好处?
【问题讨论】:
-
你认为
-"SomeString"是什么? -
如果
x是String(顺便说一句,您不应该将字符串与==进行比较。),那么-x应该是什么? -
@QBrute 你是对的。我的错。我现在更新了问题以使用 equalTo,并将 -x 更改为 y。如果它是 -x(或 y,现在),它将关联到第二种情况,其中第一种情况是 x。 x 可以在代码的开头定义为 String x = "It is cheese" 和 String y = "It's not cheese. I don't know what it is"。
-
Why doesn't this method work? Java ternary operator。 ternary operator not working。 Java ternary operator (?:) doesn't work; second or third operand return boolean。 Ternary operator to return value- Java/Android。 How to use ternary operator with new?What is the Java ?: operator called and what does it do?
标签: java if-statement conditional