解题.逻辑运算符.&, !, |, ^

 1 package com.java7;
 2 // Demonstrate the relational and logical operators.
 3 public class RelLogOps {
 4     public static void main(String[] args) {
 5         int i, j;
 6         boolean b1, b2;
 7
 8         i = 10;
 9         j = 11;
10
11         if(i < j) System.out.println("i < j");
12         if(i <= j) System.out.println("i <= j");
13         if(i != j) System.out.println("i != j");
14         if(i == j) System.out.println("i == j"); // this won‘t execute
15         if(i >= j) System.out.println("i >= j"); // this won‘t execute
16         if(i > j) System.out.println("i > j");    // this won‘t execute
17
18         b1 = true;
19         b2 = false;
20         if(b1 & b2) System.out.println("b1 & b2 is true"); // false
21         if(!(b1 & b2)) System.out.println("!(b1 & b2) is true"); // true: !true = false; !false = true(反转;只要有一个true)
22         if(b1 | b2) System.out.println("b1 | b2 is true"); // true: true  或false返回true(只要有一个true)
23         if(b1 ^ b2) System.out.println("b1 ^ b2 is true"); // true: true异或false返回true(有且只有一个true)
24
25         System.out.println();
26
27         b1 = false;
28         b2 = true;
29         if(b1 & b2) System.out.println("b1 & b2 is true"); // false
30         if(!(b1 & b2)) System.out.println("!(b1 & b2) is true"); // true: !false = true; !true = false(反转;只要有一个true)
31         if(b1 | b2) System.out.println("b1 | b2 is true"); // true: false  或true返回ture(只要有一个true)
32         if(b1 ^ b2) System.out.println("b1 ^ b2 is true"); // true: false异或true返回true(有且只有一个true)
33
34         System.out.println();
35
36         b1 = true;
37         b2 = true;
38         if(b1 & b2) System.out.println("b1 & b2 is true"); // true
39         if(!(b1 & b2)) System.out.println("!(b1 & b2) is true"); // false: !true = false; !true = false(反转;只要有一个true)
40         if(b1 | b2) System.out.println("b1 | b2 is true"); // true:  true  或 true返回true(只要有一个true)
41         if(b1 ^ b2) System.out.println("b1 ^ b2 is true"); // false: true异或 true返回 false(有且只有一个true)
42
43         System.out.println();
44
45         b1 = false;
46         b2 = false;
47         if(b1 & b2) System.out.println("b1 & b2 is true"); // false
48         if(!(b1 & b2)) System.out.println("!(b1 & b2) is true"); // true: !false = true; !false = true(反转;只要有一个true)
49         if(b1 | b2) System.out.println("b1 | b2 is true"); // false: false  或false返回false(只要有一个true)
50         if(b1 ^ b2) System.out.println("b1 ^ b2 is true"); // false: false异或false返回false(有且只有一个true)
51     }
52 }
时间: 2024-10-10 21:09:12