Logout

Conditionals Explained via a Conditionals Program

 

ConditionsPractice.java
/Users/adelaide/Public/Netbeans - All JSR Projects/DRILL AND PRACTICE - John/src/drillandpracticepackage/ConditionsPractice.java
 1 
 2 package drillandpracticepackage;
 3 
 4 import java.io.BufferedReader;
 5 import java.io.InputStreamReader;
 6 
 7 /**
 8  *
 9  * @author John Rayworth
10  */
11 public class ConditionsPractice {
12 
13     public static void main(String[] args) {
14         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15         try {
16             System.out.println("Welcome to our Conditionals Lesson");
17             System.out.println("");
18             System.out.println("if(true)");
19             if (true) {
20                 System.out.println("The condition (true) was true");
21             } else {
22                 System.out.println("The condition (true) was false.");
23             }
24             System.out.println("");
25             System.out.println("-----------------------");
26 
27 
28             System.out.println("Type in a boolean value for variable b1: (true/false).");
29             boolean b1 = Boolean.parseBoolean(br.readLine());
30           
31             System.out.println("");
32 
33             if (b1) {
34                 System.out.println("The condition (b1) was true");
35             } else {
36                 System.out.println("The condition (b1) was false.");
37             }
38 
39             System.out.println("");
40             System.out.println("");
41             System.out.println("-----------------------");
42 
43             System.out.println("Type in a boolean value for variable b2: (true/false).");
44             boolean b2 = Boolean.parseBoolean(br.readLine());
45             System.out.println("Type in a boolean value for variable b3: (true/false).");
46             boolean b3 = Boolean.parseBoolean(br.readLine());
47 
48             
49             System.out.println("");
50             if (b2 && b3) {// && means AND
51                 System.out.println("Both b2 and b3 are true.");
52             }else{
53                 System.out.println("At least one of b2 and b3 is false");
54             }
55 
56             if (b2 || b2) {// || means OR
57                 System.out.println("At least one of b2 and b3 are true.");
58             }else{
59                 System.out.println("Neither b2 nor b3 are true");
60             }
61 
62             if (b2 ^ b3) {// ^ means XOR one or the other but not both
63                 System.out.println("Either b2 or b3 was true, but not both of them.");
64             }else{
65                 System.out.println("b2 and b3 are the same; either both true or both false.");
66             }
67 
68 
69         } catch (Exception e) {
70             System.out.println("Error in main method.");
71         }
72 
73     }
74 }
75 
76