Write a Java Program to Check if two of three boolean variables are true

Write a Java Program to Check if two of three boolean variables are true

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Java Program to check if 2 variables
// among the 3 variables are true

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    
    // create 3 boolean variables
    boolean first;
    boolean second;
    boolean third;
    boolean result;

    // get boolean input from the user
    Scanner input = new Scanner(System.in);
    System.out.print("Enter first boolean value: ");
    first = input.nextBoolean();

    System.out.print("Enter second boolean value: ");
    second = input.nextBoolean();

    System.out.print("Enter third boolean value: ");
    third = input.nextBoolean();

    // check if two are true
    if(first) {

      // if first is true
      // and one of the second and third is true
      // result will be true
      result = second || third;
    }
    else {

      // if first is false
      // both the second and third should be true
      // so result will be true
      result = second && third;
    }

    if(result) {
      System.out.println("Two boolean variables are true.");
    }
    else {
      System.out.println("Two boolean variables are not true.");
    }

    input.close();
  }

}

Final Output

Leave a Comment