真值表 - 将布尔值转换为1和0

时间:2015-10-30 09:52:59

标签: java binary boolean-logic truthtable

我目前正在阅读工作手册,Java A初学者指南。第2章有一个创建真值表的小项目。显示的值的格式为true或false。目标是显示1&0和0。

我已尝试使用以下代码执行此操作,但String.valueOf方法不适用于所有表达式。我不知道如何做到这一点。

package ChapterTwo;

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

    boolean p, q;

    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

    p = true; q = true;

    String str0 = String.valueOf(p);
    String str1 = String.valueOf(q);

    str0 = true ? "1" : "2";
    str1 = true ? "1" : "2";

    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(str0 + "\t" + str1 +"\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));


    }
}

7 个答案:

答案 0 :(得分:3)

你写错了:

str0 = true ? "1" : "2";
str1 = true ? "1" : "2";

你可能意味着:

str0 = (str0 == "true") ? "1" : "2";
str1 = (str1 == "true") ? "1" : "2";

但您甚至可以更快地执行此操作(并在创建str0str1时删除首次分配):

str0 = p ? "1" : "2";
str1 = q ? "1" : "2";

编辑:另请注意,更改str0str1的值时,您必须重新计算pq你想要。所以你可能想写一个子函数@DThought回答。

编辑2 :子功能可能如下:

static String printBoolean(boolean b) {
    return b ? "1" : "2";
}

在主代码中使用此功能,如下所示:

System.out.print(printBoolean(p&q) + "\t" + printBoolean(p|q) + "\t");

另请注意,您不需要将同一段代码写入4次。您可以创建循环来迭代(p,q)的每个值。

答案 1 :(得分:0)

尝试制作以下方法: private static String convert(boolean bool);

以某种方式设置该方法,它将返回“0”和“1”表示false和true,然后在任何地方的打印结构中使用该方法,在那里你要剔除布尔值。

答案 2 :(得分:0)

感谢大家的提示和提示,最后我的最终代码如下所示。 我知道可能有更简单的方法可以做到这一点,但本书要求你利用前两章中的所有内容,所以我也包含了一个控制语句。

如果您认为可以,请投票。

由于 //试试这个2-2:逻辑运算符的真值表。 包ChapterTwo;

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

boolean p, q;
    int a = 0, b = 0;

System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

p = true; q = true;
    if(p) a = 1;
    else a = 0;
    if(q) b = 1;
    else b = 0;
    System.out.print(a + "\t" + b +"\t");
    System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
    System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));

p = true; q = false;
    if(p) a = 1;
    else a = 0;
    if(q) b = 1;
    else b = 0;
    System.out.print(a + "\t" + b +"\t");
    System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
    System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));

p = false; q = true;
    if(p) a = 1;
    else a = 0;
    if(q) b = 1;
    else b = 0;
    System.out.print(a + "\t" + b +"\t");
    System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
    System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));

p = false; q = false;
    if(p) a = 1;
    else a = 0;
    if(q) b = 1;
    else b = 0;
    System.out.print(a + "\t" + b +"\t");
    System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
    System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
}
public static String printBoolean(boolean p) {
    return p ? "1" : "0";
}
public static String printBoolean2(boolean q) {
    return q ? "1" : "0";
}
}

答案 3 :(得分:0)

经过长时间的裁员,我现在明白盖尔的评论和答案要好得多,我不知道我怎么会如此愚蠢。请参阅下面的更新代码,请注册后备份。谢谢,

package ChapterTwo;

 // Try this 2-2: a truth table for the logical operators. package   ChapterTwo;

public class LogicalOpTable {

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(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));

p = true; q = false;

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

p = false; q = true;

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

p = false; q = false;

System.out.print(printBoolean(p) + "\t" + (printBoolean2(q)) +"\t");
System.out.print(printBoolean(p&q) + "\t" + printBoolean (p|q) + "\t");
System.out.println(printBoolean2(p^q) + "\t" + printBoolean (!p));
}
public static String printBoolean(boolean p) {
return p ? "1" : "0";
}
public static String printBoolean2(boolean q) {
    return q ? "1" : "0";

    }
}

答案 4 :(得分:0)

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

    boolean p, q;
    String a;

    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

    p = true;
    q = true;
    a = ((p ? 1 : 0) + "\t" + (q ? 1 : 0) +"\t" + ((p&q)? 1 : 0) + "\t" + ((p|q)? 1:0) + "\t" + ((p^q)? 1 :0) + "\t" + ((!p)? 1 :0));
    System.out.println(a);

    p = true;
    q = false;
    System.out.println((p ? 1 : 0) + "\t" + (q ? 1 : 0) +"\t" + ((p&q)? 1 : 0) + "\t" + ((p|q)? 1:0) + "\t" + ((p^q)? 1 :0) + "\t" + ((!p)? 1 :0));

    p = false;
    q = true;
    System.out.println((p ? 1 : 0) + "\t" + (q ? 1 : 0) +"\t" + ((p&q)? 1 : 0) + "\t" + ((p|q)? 1:0) + "\t" + ((p^q)? 1 :0) + "\t" + ((!p)? 1 :0));

    p = false;
    q = false;
    System.out.println((p ? 1 : 0) + "\t" + (q ? 1 : 0) +"\t" + ((p&q)? 1 : 0) + "\t" + ((p|q)? 1:0) + "\t" + ((p^q)? 1 :0) + "\t" + ((!p)? 1 :0));
    }
}

答案 5 :(得分:0)

我也在阅读这本书,并且很失望地发现在书中或可以下载的Oracle列表中没有回答这个问题。

Anywho,我是一个完全程序员的菜鸟。上周刚刚开始阅读这本书,完全陷入了这个问题。

这是我能想到的最好的解决方案,只使用我从书中学到的东西(2-2问题6)

class LogicalOpTable6 {
public static void main(String args[]) {
    boolean p, q;

    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

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

    /* 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;
    if(p)
        System.out.print("1" + "\t");
    if(!p)
        System.out.print("0" + "\t");
    if(q)
        System.out.print("1" + "\t");
    if(!q)
        System.out.print("0" + "\t");
    if(p&q)
        System.out.print("1" + "\t");
    if(!(p&q))
        System.out.print("0" + "\t");
    if(p|q)
        System.out.print("1" + "\t");
    if(!(p|q))
        System.out.print("0" + "\t");
    if(p^q)
        System.out.print("1" + "\t");
    if(!(p^q))
        System.out.print("0" + "\t");
    if(!p)
        System.out.println("1");
    if(!(!p))
        System.out.println("0");

    /* 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;
    if(p)
        System.out.print("1" + "\t");
    if(!p)
        System.out.print("0" + "\t");
    if(q)
        System.out.print("1" + "\t");
    if(!q)
        System.out.print("0" + "\t");
    if(p&q)
        System.out.print("1" + "\t");
    if(!(p&q))
        System.out.print("0" + "\t");
    if(p|q)
        System.out.print("1" + "\t");
    if(!(p|q))
        System.out.print("0" + "\t");
    if(p^q)
        System.out.print("1" + "\t");
    if(!(p^q))
        System.out.print("0" + "\t");
    if(!p)
        System.out.println("1");
    if(!(!p))
        System.out.println("0");

    /* 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;
    if(p)
        System.out.print("1" + "\t");
    if(!p)
        System.out.print("0" + "\t");
    if(q)
        System.out.print("1" + "\t");
    if(!q)
        System.out.print("0" + "\t");
    if(p&q)
        System.out.print("1" + "\t");
    if(!(p&q))
        System.out.print("0" + "\t");
    if(p|q)
        System.out.print("1" + "\t");
    if(!(p|q))
        System.out.print("0" + "\t");
    if(p^q)
        System.out.print("1" + "\t");
    if(!(p^q))
        System.out.print("0" + "\t");
    if(!p)
        System.out.println("1");
    if(!(!p))
        System.out.println("0");

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

很多行,我很清楚,但在输入真值表第一行的if语句后,可以将其复制到其他三行。

答案 6 :(得分:0)

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

                System.out.println("P\tQ\tAND\tOR\tXOR\tNOT(P)");

                    boolean P, Q;
            
           
                P = true;
                Q = true;

                    if(P) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P&Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P|Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P^Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(!P) {
                           System.out.print("1\n");
                    }
                    else { System.out.print("0\n");
                    }
                    
                    
                P = true;
                    Q = false;
            
                    if(P) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P&Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P|Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P^Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(!P) {
                           System.out.print("1\n");
                    }
                    else { System.out.print("0\n");
                    }
                    

            P = false;
                    Q = true;
            
                    if(P) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P&Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P|Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P^Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(!P) {
                           System.out.print("1\n");
                    }
                    else { System.out.print("0\n");
                    }
                    

                P = false;
                Q = false;
            
                    if(P) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P&Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P|Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(P^Q) {
                           System.out.print("1\t");
                    }
                    else { System.out.print("0\t");
                    }
                    if(!P) {
                           System.out.print("1\n");
                    }
                    else { System.out.print("0\n");
                    }
}
}