java如何修改此真值表,使其使用并显示1和0,而不是true和false
如何修改此真值表,使其使用并显示1和0,而不是true和false
public class Table {
public static void main(String args[]){
boolean p,q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
p = true;
q = true;
System.out.print(p+"\t"+q+"\t");
System.out.print((p&q)+"\t"+(p|q)+"\t");
System.out.println((p^q)+"\t"+(!p));
p = true;
q = false;
System.out.print(p+"\t"+q+"\t");
System.out.print((p&q)+"\t"+(p|q)+"\t");
System.out.println((p^q)+"\t"+(!p));
p = false;
q = true;
System.out.print(p+"\t"+q+"\t");
System.out.print((p&q)+"\t"+(p|q)+"\t");
System.out.println((p^q)+"\t"+(!p));
p = false;
q = false;
System.out.print(p+"\t"+q+"\t");
System.out.print((p&q)+"\t"+(p|q)+"\t");
System.out.println((p^q)+"\t"+(!p));
}
}
# 1 楼答案
一个简单的解决方案是使用函数
toDigit(boolean value)
,它为false
返回0,为true
返回1;这可以在打印命令中使用,而不仅仅是打印布尔值,例如# 2 楼答案
这是一个简单的挑战,来自Herbert Schildt的《Java:初学者指南-第五版》。因此,我认为给出完整的解决方案没有问题。以下是布拉拉萨阿德里建议的改编
# 3 楼答案
在Java中,没有逻辑异或运算符,只有一个用于按位操作
参见Oracle.com - Operators作为参考
无论如何,这里是从
true
和false
字符串到1
和0
的所需转换:输出为
# 4 楼答案
一个简单的方法是: