练习.显示逻辑运算符的真值表&(显示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; 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
时间: 2024-11-04 23:16:29

练习.显示逻辑运算符的真值表&(显示true,false → 显示1,0)的相关文章

console.log(([])?true:false); console.log(([]==false?true:false)); console.log(({}==false)?true:false)

下面是题目的类型转换结果: Boolean([]); //true Number([]); //0 Number({}); // NaN Number(false); //0 因此: console.log(([])?true:fasle);// => console.log((true)?true:false); console.log([]==false?true:false); // => console.log(0==0?true:false); console.log(({}==fa

《连载 | 物联网框架ServerSuperIO教程》- 13.自定义视图显示接口开发,满足不同的显示需求

1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架ServerSuperIO教程>2.服务实例的配置参数说明 <连载 | 物联网框架ServerSuperIO教程>- 3.设备驱动介绍 <连载 | 物联网框架ServerSuperIO教程>-4.如开发一套设备驱动,同时支持串口和网络通讯. <连载 | 物联网框架ServerSupe

转:zTree树控件key配置之title:zTree树节点名称过长如何省略显示且鼠标移入节点上能够显示全称

当树节点的名称有些很长时,全部显示出来显得很拥挤的情况下,我们会想到用省略节点名称来代替,当鼠标移入节点时能够显示该节点的全称.这样我们应该如何做呢? 首先,我们要在树的节点内多增加一个属性用于设置该节点的全称,这里我们以title名称为例吧,示例代码如下所示: view sourceprint? 1.{ id:233, pId:23, name:"叶子节点233...",title:"叶子节点23333434343434"}, 2.{ id:234, pId:23

c++ 中 BOOL与bool TRUE与true FALSE与false 区别 (转载)

http://blog.chinaunix.net/uid-28458801-id-3941112.html FALSE/TRUE与false/true的区别 1.FALSE/TRUE与false/true的区别: false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途 是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义: #ifndef FALSE #define FALSE 0 #endif #ifndef

linux 查找指定内容并显示指定行数的命令,显示匹配行和行号

grep -i "desktop-printing-0.19-20.2.el5.x86_64" -n -A 10 install.log linux 查找指定内容并显示指定行数的命令,显示匹配行和行号,布布扣,bubuko.com

java显示本地磁盘所有盘符,显示桌面路径

import java.io.File; import javax.swing.filechooser.FileSystemView; /** 显示本地磁盘根盘符,显示桌面路径 */ public class RDDemo { static File[] files; public static void main(String[] args) { FileSystemView sys = FileSystemView.getFileSystemView(); files = sys.getRo

perl true/false

perl treat 0 false, non 0 ture. print "false\n" if 0; print "false\n" if 0; ------------------------ ture perl treat string empty is false. my $temp = ""; print "This is $temp\n" if $temp; $temp = "HelloWorld&q

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token &#39;name&#39;: was expecting (&#39;true&#39;, &#39;false&#39; or &#39;null&#39;)

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting ('true', 'false' or 'null') Ajax跨域问题 ajax提交添加下面两行代码 contentType:'application/json;charset=utf-8' data:JSON.stringify(数据) var allData = { name:"张三"

Javascript 内置值、typeof运算符、true/false判断

一.内置值 true false null undefined NaN Infinity 二.typeof运算结果 number string boolean undefined function object  (array.) 三.true/false true: 字符串.true.对象 false: