Java 必知必会 第 2 篇
给3个布尔变量,当其中有2个或者2个以上为true才返回true
问题
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
最笨的方法:
boolean atLeastTwo(boolean a
, boolean b
, boolean c
)
{
if ((a
&& b
) || (b
&& c
) || (a
&& c
))
{
return true;
}
else
{
return false;
}
}
优雅解法1
return a
? (b
|| c
) : (b
&& c
);
优雅解法2
return (a
==b
) ? a
: c
;
优雅解法3
return a
^ b
? c
: a
优雅解法4
return a
? (b
|| c
) : (b
&& c
);
转载请注明原文地址: https://mac.8miu.com/read-488706.html