有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

共 (4) 个答案

  1. # 1 楼答案

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

    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));
    
  2. # 2 楼答案

    这是一个简单的挑战,来自Herbert Schildt的《Java:初学者指南-第五版》。因此,我认为给出完整的解决方案没有问题。以下是布拉拉萨阿德里建议的改编

    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;
        }
    }
    
  3. # 3 楼答案

    在Java中,没有逻辑异或运算符,只有一个用于按位操作

    参见Oracle.com - Operators作为参考


    无论如何,这里是从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   
    
  4. # 4 楼答案

    一个简单的方法是:

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