JavaScript
x
Boolean a, b, c, d;
I would like to count the number of trues, each result should have its own associated action. Maybe something like:
JavaScript
int result = getResult(a, b, c, d);
switch (result) {
case 0: break;
case 1: break;
case 2: break;
case 3: break;
default: break;
}
Any idea of how to write the getResult
method body a pretty way? In the example, I used only four, but it should be extendable to a bigger number of booleans. Any other way to proceed is welcome.
Advertisement
Answer
Write a variadic method?
JavaScript
int getResult(boolean vars) {
int count = 0;
for (boolean var : vars) {
count += (var ? 1 : 0);
}
return count;
}