如何更改所打印内容的输出?

时间:2016-07-06 21:07:25

标签: java

我有这个代码打印出一个逻辑表 它打印出这张表。 P Q AND OR XOR NOTp true true true true false false true false false true true false false true false true true 虚假虚假虚假虚假真实 我试图将所有输出的true和false更改为1和0.第一行看起来像这样。 P Q AND OR XOR NOTp 1 1 1 1 0 0

我甚至不知道从哪里开始。我试过声明。任何帮助将不胜感激。

public class LogicalOpTable {
    public static void main(String[] args) {
        boolean p, q;
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");

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

3 个答案:

答案 0 :(得分:0)

要为布尔$scope.pivotTable1打印0true,您应该将结果从布尔值转换为int,例如像:

false

这是

的语法糖
System.out.println((a|b) ? 1 : 0);

答案 1 :(得分:0)

尝试使用此方法,如果print的参数为true,则返回1,否则为0

Console.WriteLine(numbers);

你的桌子是正确的

输出:

public static void main(String[] args) {
    boolean p, q;
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");

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

public static String print(boolean stat) {
    if(stat) return "1";        // if stat is true
    return "0";                 // else false
}

答案 2 :(得分:0)

public class LogicalOpTable {

public static void main(String[] args) {
     boolean p, q;
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");

      p = true; q = true;
      String output=(p&q) + "\t" + (p|q) + "\t";output=output.replace("true", "1").replace("false", "0");
      System.out.print(output);
      output=repl((p&q) + "\t" + (p|q) + "\t");
      System.out.print(output);
      output=repl((p^q) + "\t" + (!p));
      System.out.println(output);
      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));
}
public static String repl(String input)
{
    return input.replace("true", "1").replace("false", "0");
}

}

<强>输出:

   P    Q   AND OR  XOR NOTp
1   1   1   1   0   0
true    false   false   true    true    false
false   true    false   true    true    true
false   false   false   false   false   true

我已经完成了一行。请为其他练习线做。