Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修改此真值表,使其使用并显示1';s和0';这不是真假_Java_Boolean_Truthtable - Fatal编程技术网

Java 如何修改此真值表,使其使用并显示1';s和0';这不是真假

Java 如何修改此真值表,使其使用并显示1';s和0';这不是真假,java,boolean,truthtable,Java,Boolean,Truthtable,如何修改此真值表,使其使用并显示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+"

如何修改此真值表,使其使用并显示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(布尔值)
,它返回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中,没有逻辑异或运算符,只有一个用于按位操作

请参阅作为参考


无论如何,这里是从
true
false
字符串到
1
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   
一个简单的方法是:

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

这是一个简单的挑战,来自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;
    }
}

我必须导入此函数才能使其工作吗?当它出现红线时,您必须编写
toDigit
函数。虽然它很短,但它需要一个布尔值,如果布尔值为
true
,它将返回
1
0
。仍然无法可视化或理解如何编写,您可以修改表以向我展示它是如何完成的吗?不需要修改表本身。在类的底部(就在最后一个
}
之前),您必须添加一个新函数,该函数的外观如下:
private static int toDigit(boolean value){…}
。如果
为真,则必须返回
1
,否则返回
0
,而不是
。如果您不知道如何编写函数,请检查,如果您不知道如何执行依赖于布尔值的操作,请尝试