标签:style blog ar color sp for java div log
原型:
1 package com.java7; 2 // Try this 2-2: a truth table for the logical operators. 3 public class LogicalOpTable { 4 public static void main(String[] args) { 5 6 boolean p, q; 7 8 System.out.println("p\tQ\tAND\tOR\tXOR\tNOT"); 9 10 p = true; q = true; 11 System.out.print(p + "\t" + q + "\t"); 12 System.out.print((p&q) + "\t" + (p|q) + "\t"); 13 System.out.println((p^q) + "\t" + (!(p&q))); 14 15 p = true; q = false; 16 System.out.print(p + "\t" + q + "\t"); 17 System.out.print((p&q) + "\t" + (p|q) + "\t"); 18 System.out.println((p^q) + "\t" + (!(p&q))); 19 20 p = false; q = true; 21 System.out.print(p + "\t" + q + "\t"); 22 System.out.print((p&q) + "\t" + (p|q) + "\t"); 23 System.out.println((p^q) + "\t" + (!(p&q))); 24 25 p = false; q = false; 26 System.out.print(p + "\t" + q + "\t"); 27 System.out.print((p&q) + "\t" + (p|q) + "\t"); 28 System.out.println((p^q) + "\t" + (!(p&q))); 29 } 30 }
执行结果:
P | Q | AND | OR | XOR | NOT |
true | true | true | true | false | false |
true | false | false | true | true | true |
false | true | false | true | true | true |
false | false | false | false | false | true |
修改为显示1,0(方法1):
1 package com.java7; 2 // Try this 2-2: a truth table for the logical operators. 3 public class LogicalOpTable10Method1 { 4 public static void main(String[] args) { 5 6 boolean p, q; 7 int o ,z; 8 o = 1; 9 z = 0; 10 11 System.out.println("p\tQ\tAND\tOR\tXOR\tNOT"); 12 13 // true; true 14 p = true; q = true; 15 if(p == true){ 16 System.out.print(o + "\t"); 17 }else{ 18 System.out.print(z + "\t"); 19 } 20 if(q == true){ 21 System.out.print(o + "\t"); 22 }else{ 23 System.out.print(z + "\t"); 24 } 25 if(p & q == true){ 26 System.out.print(o + "\t"); 27 }else{ 28 System.out.print(z + "\t"); 29 } 30 if(p | q == true){ 31 System.out.print(o + "\t"); 32 }else{ 33 System.out.print(z + "\t"); 34 } 35 if(p ^ q == true){ 36 System.out.print(o + "\t"); 37 }else{ 38 System.out.print(z + "\t"); 39 } 40 if(!(p & q) == true){ 41 System.out.println(o + "\t"); 42 }else{ 43 System.out.println(z + "\t"); 44 } 45 46 // true; false 47 p = true; q = false; 48 if(p == true){ 49 System.out.print(o + "\t"); 50 }else{ 51 System.out.print(z + "\t"); 52 } 53 if(q == true){ 54 System.out.print(o + "\t"); 55 }else{ 56 System.out.print(z + "\t"); 57 } 58 if(p & q == true){ 59 System.out.print(o + "\t"); 60 }else{ 61 System.out.print(z + "\t"); 62 } 63 if(p | q == true){ 64 System.out.print(o + "\t"); 65 }else{ 66 System.out.print(z + "\t"); 67 } 68 if(p ^ q == true){ 69 System.out.print(o + "\t"); 70 }else{ 71 System.out.print(z + "\t"); 72 } 73 if(!(p & q) == true){ 74 System.out.println(o + "\t"); 75 }else{ 76 System.out.println(z + "\t"); 77 } 78 79 80 // false; true 81 p = false; q = true; 82 if(p == true){ 83 System.out.print(o + "\t"); 84 }else{ 85 System.out.print(z + "\t"); 86 } 87 if(q == true){ 88 System.out.print(o + "\t"); 89 }else{ 90 System.out.print(z + "\t"); 91 } 92 if(p & q == true){ 93 System.out.print(o + "\t"); 94 }else{ 95 System.out.print(z + "\t"); 96 } 97 if(p | q == true){ 98 System.out.print(o + "\t"); 99 }else{ 100 System.out.print(z + "\t"); 101 } 102 if(p ^ q == true){ 103 System.out.print(o + "\t"); 104 }else{ 105 System.out.print(z + "\t"); 106 } 107 if(!(p & q) == true){ 108 System.out.println(o + "\t"); 109 }else{ 110 System.out.println(z + "\t"); 111 } 112 113 114 // false; false 115 p = false; q = false; 116 if(p == true){ 117 System.out.print(o + "\t"); 118 }else{ 119 System.out.print(z + "\t"); 120 } 121 if(q == true){ 122 System.out.print(o + "\t"); 123 }else{ 124 System.out.print(z + "\t"); 125 } 126 if(p & q == true){ 127 System.out.print(o + "\t"); 128 }else{ 129 System.out.print(z + "\t"); 130 } 131 if(p | q == true){ 132 System.out.print(o + "\t"); 133 }else{ 134 System.out.print(z + "\t"); 135 } 136 if(p ^ q == true){ 137 System.out.print(o + "\t"); 138 }else{ 139 System.out.print(z + "\t"); 140 } 141 if(!(p & q) == true){ 142 System.out.println(o + "\t"); 143 }else{ 144 System.out.println(z + "\t"); 145 } 146 } 147 }
执行结果:
P | Q | AND | OR | XOR | NOT |
1 | 1 | 1 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 1 |
0 | 1 | 0 | 1 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 1 |
练习.显示逻辑运算符的真值表&(显示true,false → 显示1,0)
标签:style blog ar color sp for java div log
原文地址:http://www.cnblogs.com/fatoland/p/4156690.html