Count the number of "trues" for n booleans

boolean, count, java

Solution

Write a variadic method?

int getResult(boolean... vars) {
    int count = 0;
    for (boolean var : vars) {
        count += (var ? 1 : 0);
    }
    return count;
}

Problem

``` 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: ``` 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.

Original source