-1

我目前正在阅读工作手册 Java A Beginners Guide。第 2 章有一个创建真值表的小项目。显示的值采用 true 或 false 格式。目标是改为显示 1 和 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));


    }
}
4

7 回答 7

3

你写错了:

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

您可能的意思是:

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

但是您甚至可以更快地完成它(并在创建str0and时删除第一个分配str1):

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

编辑:另请注意,您必须重新计算str0以及str1在更改值时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).

于 2015-10-30T09:55:09.147 回答
0

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

以某种方式设置该方法,它将为您返回“0”和“1”以表示假和真,然后在您打印布尔值的任何地方的打印构造中使用该方法。

于 2015-10-30T09:57:41.597 回答
0

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

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

谢谢 // 试试这个 2-2:逻辑运算符的真值表。包第二章;

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";
}
}
于 2015-11-03T15:58:59.587 回答
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";

    }
}
于 2016-05-27T15:05:43.663 回答
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));
    }
}
于 2017-03-05T22:55:06.097 回答
0

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

无论如何,我是一个完全的程序员菜鸟。上周刚开始看这本书,完全被这个问题困住了。

这是我能想到的最好的解决方案,只使用我从书中学到的东西(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 语句后,可以将它们复制到其他三行。

于 2017-08-24T13:35:20.780 回答
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");
                    }
}
}
于 2020-11-03T02:39:11.553 回答