0

如何修改此真值表,使其使用和显示 1 和 0 而不是真值和假值。

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));
    }
}
4

4 回答 4

1

一个简单的解决方案是有一个函数toDigit(boolean value),它返回 0 forfalse和 1 for 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));
于 2014-04-07T12:54:29.817 回答
1

在Java中,不存在逻辑异或运算符,只有一个用于按位运算!

请参阅Oracle.com - Operators as reference


无论如何,这里是您想要的从trueandfalse字符串到1and的转换0

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   
于 2014-04-07T13:02:16.160 回答
0

一种简单的方法是:

boolean flag;
//...
System.out.println(flag ? 1 : 0);
于 2014-04-07T13:02:22.007 回答
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;
    }
}
于 2015-10-24T15:59:22.263 回答