Blake Havens Blake Havens - 1 year ago 58
Java Question

Checking amount of booleans that are true, and executing differently depending on the amount

So I'm trying to check multiple booleans (6), and want to execute different code if more than one, two, three, etc. are true.

So here is an example:

public static boolean x;
public static boolean y;
public static boolean z;
public static boolean a;
public static boolean b;
public static boolean c;
public static int amtTrue;
//if x & y are true, then set amtTrue to 1
//if y & z AND x & y are true, then set amtTrue to 2;
//keep iterating though all possiblilites

What would be the most efficient way to do this?

Thanks for your time!

Answer Source
int i = 0;

for(boolean b : array)
    if(b) i++;

case 0:

case 1:

case 2:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download