繁体   English   中英

如何修改此真值表以便它使用并显示1和0而不是true和false

[英]How do I modify this truth table so that it uses and displays 1's and 0's rather than true and 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));
    }
}

一个简单的解决方案是使用函数toDigit(boolean value) ,其返回0表示false ,1表示true ; 然后可以在打印命令中使用它,而不仅仅是打印布尔值,例如

System.out.print(toDigit(p)+"\t"+toDigit(q)+"\t");
System.out.print(toDigit(p&q)+"\t"+toDigit(p|q)+"\t");
System.out.println(toDigit(p^q)+"\t"+toDigit(!p));

在Java中, 存在逻辑XOR运算符,只有一个运算!

请参阅Oracle.com - 运算符作为参考


无论如何,从这里你想要转换truefalse字符串,以10

public static void main(String[] args) {
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT(p)");

    printTable(true, true);
    printTable(true, false);
    printTable(false, true);
    printTable(false, false);
}

private static void printTable(final boolean p, final boolean q) {
    System.out.printf("%s\t", p    ? "1" : "0");
    System.out.printf("%s\t", q    ? "1" : "0");
    System.out.printf("%s\t", p&&q ? "1" : "0");
    System.out.printf("%s\t", p||q ? "1" : "0");
    System.out.printf("%s\t", p^q  ? "1" : "0");
    System.out.printf("%s\t", !p   ? "1" : "0");
    System.out.printf("\n");
}

输出是

P       Q       AND     OR      XOR     NOT (p)
1       1       1       1       0       0   
1       0       0       1       1       0   
0       1       0       1       1       1   
0       0       0       0       0       1   

一个简单的方法是:

boolean flag;
//...
System.out.println(flag ? 1 : 0);

这是Herbert Schildt撰写的“Java:A Beginner's Guide - Fifth Edition”一书中的一个简单挑战。 因此,我认为提供完整的解决方案没有问题。 以下是blalasaadri建议的适应性礼貌。

public class Table {
    public static void main(String[] args) {

        boolean p, q;

        // below is an alternative truth table in binary form (0s and 1s)
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

        p = true; q = true;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = true; q = false;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = false; q = true;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = false; q = false;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");
    }

    // this method exchanges true for 1, and false for 0 
    private static int toBinary(boolean value) {
        if(value == true)
            return 1;
        else
            return 0;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM