【问题标题】:How do I modify this truth table so that it uses and displays 1's and 0's rather than true and false如何修改此真值表,使其使用并显示 1 和 0 而不是真值和假值
【发布时间】:2014-04-07 12:52:09
【问题描述】:

如何修改此真值表,使其使用和显示 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));
    }
}

【问题讨论】:

    标签: java boolean truthtable


    【解决方案1】:

    在 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   
    

    【讨论】:

      【解决方案2】:

      一个简单的解决方案是有一个函数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));
      

      【讨论】:

      • 我必须导入这个函数才能让它工作吗?因为它出现了红线
      • 您必须编写toDigit 函数。虽然它很短 - 它需要一个布尔值,如果布尔值是 true,它将返回 10 否则。
      • 仍然无法想象或理解如何编写,您能否修改表格让我看看它是如何完成的?
      • 不需要修改表本身。在类的底部(就在最后一个} 之前),您必须添加一个看起来像这样的新函数:private static int toDigit(boolean value) { ... }。如果value 为真,则必须返回1 而不是...,否则返回0。如果您不知道如何编写函数,请查看docs.oracle.com/javase/tutorial/java/javaOO/methods.html,如果您不知道如何根据布尔值执行某些操作,请尝试docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
      【解决方案3】:

      一种简单的方法是:

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

      【讨论】:

        【解决方案4】:

        这是 Herbert Schildt 的《Java:初学者指南 - 第五版》一书中的一个简单挑战。因此,我认为给出完整的解决方案没有问题。以下是根据 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;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-25
          • 2016-06-12
          • 1970-01-01
          • 2019-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多