Java program to check if two of three boolean variables are true

http‮ww//:s‬w.theitroad.com

Here's an example Java program to check if two of three boolean variables are true:

public class BooleanChecker {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean c = true;

        int count = 0;

        if (a) {
            count++;
        }
        if (b) {
            count++;
        }
        if (c) {
            count++;
        }

        if (count == 2) {
            System.out.println("Two of the three boolean variables are true");
        } else {
            System.out.println("Not exactly two of the three boolean variables are true");
        }
    }
}

This program declares three boolean variables a, b, and c and sets their values to true, false, and true, respectively. The program then uses a counter variable count to count the number of true values among the three boolean variables. The count variable is incremented by one for each true value using separate if statements for each boolean variable.

Finally, the program checks if the count variable equals 2, which means that exactly two of the three boolean variables are true. If this condition is true, the program prints a message to the console indicating that two of the three boolean variables are true. Otherwise, the program prints a message indicating that not exactly two of the three boolean variables are true.

Note that this program assumes that only three boolean variables need to be checked. If you need to check a larger number of boolean variables, you could use a loop to iterate over the variables and count the number of true values.