Java – calculates the number of “trues” of N Boolean values
•
Java
Boolean a,b,c,d;
Boolean a,b,c,d;
I want to count the number of trues. Each result should have its own associated action Maybe it's like this:
int result = getResult(a,d);
switch (result) {
case 0: break;
case 1: break;
case 2: break;
case 3: break;
default: break;
}
Is it nice to know how to write the getResult method body? In this example, I only use four, but it should be able to expand to more Boolean values You are welcome to continue in any other way
Solution
Write a variable method?
int getResult(boolean... vars) {
int count = 0;
for (boolean var : vars) {
count += (var ? 1 : 0);
}
return count;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
