首页 > 代码库 > 练习.显示逻辑运算符的真值表&(显示true,false → 显示1,0)
练习.显示逻辑运算符的真值表&(显示true,false → 显示1,0)
原型:
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; false115 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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。