Java 如何更改what';什么是印刷品?
我有一个打印逻辑表的代码。Java 如何更改what';什么是印刷品?,java,Java,我有一个打印逻辑表的代码。 它打印出这张桌子。 P Q与或异或NOTp 真假 真假假真假 假真假真真 假假假假真 我正在尝试将所有输出的true和false更改为1和0。第一行是这样的。 P Q与或异或NOTp 1100 我甚至不知道从哪里开始。我试过if语句。任何帮助都将不胜感激 public class LogicalOpTable { public static void main(String[] args) { boolean p, q; Sys
它打印出这张桌子。 P Q与或异或NOTp 真假 真假假真假 假真假真真 假假假假真 我正在尝试将所有输出的true和false更改为1和0。第一行是这样的。 P Q与或异或NOTp 1100 我甚至不知道从哪里开始。我试过if语句。任何帮助都将不胜感激
public class LogicalOpTable {
public static void main(String[] args) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");
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));
}
}
要打印布尔值
true
或false
的1
或0
,应将结果从布尔值转换为int,例如:
System.out.println((a|b) ? 1 : 0);
这是糖的语法
if(a|b){
System.out.println(1);
} else {
System.out.println(0);
}
尝试使用此选项,如果打印参数为真,则返回1,否则返回0
public static void main(String[] args) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");
p = true; q = true;
System.out.print(print(p) + "\t" + print(q) +"\t");
System.out.print(print(p&q) + "\t" + print(p|q) + "\t");
System.out.println(print(p^q) + "\t" + print(!p));
p = true; q = false;
System.out.print(print(p) + "\t" + print(q) +"\t");
System.out.print(print(p&q) + "\t" + print(p|q) + "\t");
System.out.println(print(p^q) + "\t" + print(!p));
p = false; q = true;
System.out.print(print(p) + "\t" + print(q) +"\t");
System.out.print(print(p&q) + "\t" + print(p|q) + "\t");
System.out.println(print(p^q) + "\t" + print(!p));
p = false; q = false;
System.out.print(print(p) + "\t" + print(q) +"\t");
System.out.print(print(p&q) + "\t" + print(p|q) + "\t");
System.out.println(print(p^q) + "\t" + print(!p));
}
public static String print(boolean stat) {
if(stat) return "1"; // if stat is true
return "0"; // else false
}
你的桌子是对的
输出:
P Q AND OR XOR NOTp
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 1
0 0 0 0 0 1
}
输出:
P Q AND OR XOR NOTp
1 1 1 1 0 0
true false false true true false
false true false true true true
false false false false false true
我已经做了一行了。请对其他行执行此操作。我不能100%确定您在这里尝试执行的操作,但
&
和|
是位运算符。您需要使用&&
和|
进行逻辑and和OR。看。逻辑不是(!)为你做这项工作you@MuhammadFarrukhFaizyOP已经在使用!p
作为每个println
中的最后一个参数,所以我敢打赌他们知道。什么是“if函数”?if语句哈哈,我的错这太棒了。非常感谢。我理解大部分代码,我只有几个问题。打印方法位于底部有什么原因吗?为什么它会替换第一个块的输出?代码不会首先运行main方法并失败吗?还有为什么if(stat)返回“1”有效,if(stat){system.out.print(“1”)不工作?对不起,我很兴奋哈哈*打印函数声明在哪里并不重要*'它为什么会替换第一个块的输出'wtf?你能再写一次吗*你需要返回一个变量这就是为什么system.out.println()不工作的原因我做了,它说它已经被记录了,但是15岁以下的选票不会公开显示):
P Q AND OR XOR NOTp
1 1 1 1 0 0
true false false true true false
false true false true true true
false false false false false true