我正在看一本Java书籍,还记得阅读过有关使用?的内容。 if语句的运算符,我再也找不到了阅读材料。我尝试搜索该主题,但没有结果。那么下面的代码如何
if( itemA ? itemB) : itemC
工作
答案 0 :(得分:0)
“?” “:”对应三元运算符
即:a ? b : c
的意思是“如果a为true,则返回b,否则返回c”
答案 1 :(得分:0)
它称为三元运算符。语法就像
(Condition) ? (Value to be assigned if the condition is true) : (Value to be assigned if the condition is false)
请看下面的示例以了解用法
例如。
public String getGender(String genderCode){
return gender.equals("M")?"Male":"Female";
}