Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4070e8b

Browse filesBrowse files
committed
Create Check-if-at-least-two-out-of-three-booleans-are-true.md
1 parent c2ebd5e commit 4070e8b
Copy full SHA for 4070e8b

File tree

Expand file treeCollapse file tree

1 file changed

+39
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+39
-0
lines changed
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
给3个布尔变量,当其中有2个或者2个以上为true菜返回true
2+
===
3+
问题
4+
给3个boolean变量,a,b,c,当其中有2个或2个以上为true时才返回true?
5+
最笨的方法:
6+
```java
7+
boolean atLeastTwo(boolean a, boolean b, boolean c)
8+
{
9+
if ((a && b) || (b && c) || (a && c))
10+
{
11+
return true;
12+
}
13+
else
14+
{
15+
return false;
16+
}
17+
}
18+
```
19+
* 优雅解法1
20+
```java
21+
return a ? (b || c) : (b && c);
22+
```
23+
24+
* 优雅解法2
25+
```java
26+
return (a==b) ? a : c;
27+
```
28+
29+
* 优雅解法3
30+
```java
31+
return a ^ b ? c : a
32+
```
33+
34+
* 优雅解法4
35+
```java
36+
return a ? (b || c) : (b && c);
37+
```
38+
39+
stackoverflow链接: http://stackoverflow.com/questions/3076078/check-if-at-least-two-out-of-three-booleans-are-true

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.