【问题标题】:Casting Boolean To Int将布尔值转换为 Int
【发布时间】:2012-02-25 04:47:57
【问题描述】:

我正在浏览一个教程,到目前为止它为您提供了以下代码:

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

任务是修改程序,使其使用1's0's 而不是truefalse

我不确定这是否可以通过 Casting Incompatible Types 来完成,但我认为这是要走的路,因为这是前面的部分。

任何人都可以就它的工作原理提供一些建议和解释吗?

【问题讨论】:

标签: java


【解决方案1】:

这是我的解决方案 //逻辑表使用1和0

类 LogicOptTable {

public static void main(String args[]) {

   int p, q;

   System.out.println("P\tQ\t"+
             "AND\tOR\tXOR\tNOT");
   for (p = 1; p >= 0; p--) {
    for (q = 1; q >= 0; q--) {

   System.out.println(p + "\t" + q + "\t" +
             (p & q) + "\t" + (p | q) + 
                     "\t" + (p ^ q) + "\t" + (1 - p));
     }
  }               

} }

【讨论】:

    【解决方案2】:

    您不能在 java 中将 int 转换为 boolean。考虑使用

    boolean x = p & q;
    boolean y = p | q;
    
    (x ? 1 : 0)
    

    例如

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

    可能变成:

    System.out.print(x + "\t" + y + "\t");
    

    【讨论】:

      【解决方案3】:

      您可以将整数与位运算符一起使用:

      int p, q;
      
      System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
      p = 1;
      q = 1;
      
      System.out.print(p + "\t" + q + "\t");
      System.out.print((p & q) + "\t" + (p | q) + "\t");
      System.out.println((p ^ q) + "\t" + (1-p));
      
      p = 1;
      q = 0;
      System.out.print(p + "\t" + q + "\t");
      System.out.print((p & q) + "\t" + (p | q) + "\t");
      System.out.println((p ^ q) + "\t" + (1-p));
      
      p = 0;
      q = 1;
      System.out.print(p + "\t" + q + "\t");
      System.out.print((p & q) + "\t" + (p | q) + "\t");
      System.out.println((p ^ q) + "\t" + (1-p));
      
      p = 0;
      q = 0;
      System.out.print(p + "\t" + q + "\t");
      System.out.print((p & q) + "\t" + (p | q) + "\t");
      System.out.println((p ^ q) + "\t" + (1-p));
      

      返回:

      P   Q   AND OR  XOR NOT
      1   1   1   1   0   0
      1   0   0   1   1   0
      0   1   0   1   1   1
      0   0   0   0   0   1
      

      【讨论】:

        【解决方案4】:

        这不是本教程要求您执行的操作。我认为他们希望您将boolean 替换为int,将true 替换为1,并将false 替换为0,如下所示:

        int p, q;
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
        p = 1; q = 1;
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p&q) + "\t" + (p|q) + "\t");
        System.out.println((p^q) + "\t" + (1-p)); // EDIT: was !p
        

        这将引导您了解整数 01 的按位运算。

        【讨论】:

        • 当我尝试这个时,我得到一个错误 (!p)The operator ! is undefined for the argument type(s) int 这本书确实指出这可能比最初想象的要复杂一些。
        • @Matt 没有与boolean ! 等效的一元(~ 翻转所有位,而不仅仅是最后一位)。实现您正在寻找的结果的最简单方法是使用(1-p)
        【解决方案5】:

        只需使用三元运算符:

        int logicalInt = boolVal? 1 : 0;
        

        其中“boolVal”是您的布尔变量。

        【讨论】:

        • 这本书还没有涉及,所以我不认为这是要走的路:S
        【解决方案6】:

        看起来您只需将变量声明为ints 并将01 分配给pq,并确保在所有情况下都使用java's bitwise operators(乍一看,它看起来像你)。 More info on bitwise operation from wikipedia.

        【讨论】:

        • 当我尝试这个时,我得到一个错误 (!p)The operator ! is undefined for the argument type(s) int 这本书确实指出这可能比最初想象的要复杂一些。
        • 正如其他人所说,您需要执行 1-p 而不是 !p。
        【解决方案7】:

        在 Java 中,您不能直接将布尔值转换为 int。我会按照

        的方式添加一个方法
        public int getBoolValue(boolean b) {
         return b ? 1 : 0
        

        }

        【讨论】:

          【解决方案8】:

          您不能将布尔值转换为 int。这些是完全不同的类型。

          但是您可以编写一个实用方法booleanToInt(boolean b) 将布尔值转换为整数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-04-17
            • 2015-09-27
            • 2016-01-19
            • 2013-02-13
            • 1970-01-01
            • 2015-05-13
            • 2016-11-21
            相关资源
            最近更新 更多