在Java中将a转换boolean为an 的最常用方法是什么int?
在Java中将a转换boolean为an 的最常用方法是什么int?
Answers:
int myInt = myBoolean ? 1 : 0;^^
PS:真= 1,假= 0
(foo && (!bar || baz)) ? 1 : 0。显然,如果只是标识符,则不需要或不需要括号。
                    (boolean expression) ? 1 : 0;这样的初学者,会更容易理解。我认为my前缀使它看起来像一个变量。
                    foo && (!bar || baz) ? 1 : 0将是语法错误。(我知道已经6年了)
                    使用三元运算符是最简单,最有效和最易读的方式来执行所需的操作。我鼓励您使用此解决方案。
但是,我忍不住提出一个替代的,人为的,低效的,不可读的解决方案。
int boolToInt(Boolean b) {
    return b.compareTo(false);
}嘿,人们喜欢投票给这些很酷的答案!
编辑
顺便说一句,我经常看到从布尔值到整数的转换,其唯一目的是比较两个值(通常在compareTo方法的实现中)。Boolean#compareTo在这些特定情况下应该采取的方法。
编辑2
Java 7引入了一个新的实用程序功能,该功能可以直接与原始类型一起使用Boolean#compare(感谢shmosel)
int boolToInt(boolean b) {
    return Boolean.compare(b, false);
}Boolean.compare()并避免自动装箱。2. for的文档Boolean.compareTo()没有说它将返回1,只是“如果此对象表示true,而参数表示false,则为正值”。
                    boolean b = ....; 
int i = -("false".indexOf("" + b));5 - b.toString().length
                    这取决于情况。通常,最简单的方法是最好的方法,因为它易于理解:
if (something) {
    otherThing = 1;
} else {
    otherThing = 0;
}要么
int otherThing = something ? 1 : 0;但是有时使用枚举而不是布尔标志很有用。假设有同步和异步过程:
Process process = Process.SYNCHRONOUS;
System.out.println(process.getCode());在Java中,枚举可以具有其他属性和方法:
public enum Process {
    SYNCHRONOUS (0),
    ASYNCHRONOUS (1);
    private int code;
    private Process (int code) {
        this.code = code;
    }
    public int getCode() {
        return code;
    }
}?:是可以将断点放在if块内。
                    如果您使用Apache Commons Lang(我认为很多项目都在使用它),则可以像这样使用它:
int myInt = BooleanUtils.toInteger(boolean_expression); toInteger如果boolean_expression为true,则方法返回1 ,否则返回0
BooleanUtils.toInteger仅实现为return bool ? 1 : 0;。
                    让我们一起玩吧Boolean.compare(boolean, boolean)。函数的默认行为:如果两个值相等,则返回0否则-1。
public int valueOf(Boolean flag) {
   return Boolean.compare(flag, Boolean.TRUE) + 1;
}说明:我们知道Boolean.compare的默认返回-1在不匹配的情况下,这样+1化妆返回值为0的False和1对True
Boolean.compare(myBoolean, false)会更适合引用的描述
                    我必须围绕if语句编写一个包装方法来强制转换值,这一事实增加了Java应该已经死掉的原因。看到三元运算符甚至没有包装在函数中,而是复制粘贴到了所有地方,我看到布尔值转换为int会使我发疯。这浪费了cpu周期,并且侮辱了代码的可读性。
同样,这里的大多数答案都有数百个带有零解释的反对,这使我猜测不良实践仅仅是暴露于Java过多的结果。如果我写了一个三元运算符,用任何现代语言将boolean转换为int,第二天我可能会被解雇。所以今天我放弃了尝试在我的Java作业中运用推理,并决定接受愚蠢:
public static int booltoint(boolean nonsense) {
                                // Let's start with indenting using spaces, 16 of them to be precise
                                String[] ____int = new String[500];
                                ____int[0] = Boolean.toString(nonsense);
                                try {
                                                Thread.sleep(100); 
                                } catch (Exception e) {    }
                                Random rand = new Random();
                                int n = rand.nextInt((int)1e9);
                                int result = 0;
                                int other_Result = 1;
                                for (int i = -5; i < 2; i++) {
                                                try {
                                                                if (n == 35467247) return 5; // let's just fail with one in a billion chance
                                                                if ((5 - ____int[i].length()) == 1) {
                                                                                return ++result;
                                                                } else if(____int[i] == "false") {
                                                                                return --other_Result;
                                                                }
                                                } catch (Exception e) {
                                                                // This method will throw an exception 5 times every single time I 
                                                                // cast a boolean to int before returning an integer
                                                }
                                }
                                return (int)1e35;}
true和的整数是什么false?