简体   繁体   English

将 int 初始化与 boolean 运算符比较 if 语句参数

[英]Comparing an int initialization with a boolean operator for an if statement parameter

I've declared ints skyB and day , and written this line:我已经声明了 ints skyBday ,并写下了这一行:

if (skyB == true || (day=1) != true) {
System.out.print("example text");  
}

Am I correct in assuming that the code will properly execute the argument given these parameters?我是否正确地假设代码将在给定这些参数的情况下正确执行参数? I'm specifically unsure about whether or not the (day="1") would be properly detected as false, given the instance that the objects input was indeed a false.我特别不确定 (day="1") 是否会被正确检测为错误,因为对象输入确实是错误的。

minimal reproducable example(not including main method or packages):最小可重现示例(不包括主要方法或包):

 int skyB = false;

 int day =2;

 if (skyB == true || (day =1) != true) {
System.out.print("example text");  }

The simple answer: this isn't C++简单的答案:这不是 C++

You cannot assign numeric value to boolean fields.您不能为 boolean 字段分配数值。 In Java, the only two valid values are true and false .在 Java 中,只有两个有效值是truefalse Therefore, you cannot assign integer values to a boolean variable.因此,您不能将 integer 值分配给 boolean 变量。

For your boolean expressions to be valid, your variables skyB and day must be boolean data types为了使您的 boolean 表达式有效,您的变量skyBday必须是boolean数据类型

boolean skyB = false
boolean day = true

if (!skyB || day) {
  System.out.print("example text");
}

The boolean fields are evaluated without the need to compare them to true or false.无需将boolean字段与 true 或 false 进行比较即可对其进行评估。 The expression !skyB (read "not skyB") evaluates to false .表达式!skyB (读作“不是 skyB”)的计算结果为false So this is the equivalent to skyB == false .所以这相当于skyB == false Since that portion evaluates to true (skyB is equal to false), the second part of the expression is skipped and the System.out.print() is called.由于该部分的计算结果为真(skyB 等于假),表达式的第二部分将被跳过并调用System.out.print() If the value of skyB was changed to true, the variable day would then be evaluated and the if-statement would be executed if the value of that variable equals true (which it is).如果skyB的值更改为 true,则将评估变量day ,如果该变量的值等于 true(它是),则将执行 if 语句。

Alternatively, you could build boolean expressions using numeric values.或者,您可以使用数值构建 boolean 个表达式。 How?如何? Simple.简单的。 Any mathematical expression is evaluated the same.任何数学表达式的计算都是一样的。 For example:例如:

int x = 5;
int y = 3;

if (x > y) {
  System.out.println("x is greater than y");
}

Basically... The logic of what you are trying to do (the way I understood it) is correct, but your implementation was not.基本上......你试图做的事情的逻辑(我理解它的方式)是正确的,但你的实施不是。 You cannnot mix boolean fields and numeric values.您不能混合使用 boolean 字段和数值。 It is simply not allowed in Java. Java 根本不允许。

Now, to your question...现在,对于你的问题...

boolean skyB = false; // this must be boolean, not int
int day =2;       // this is fine

if (skyB || day == 1) {
  System.out.print("example text");
}

Should produce the desired outcome.应该产生预期的结果。 In plain English, the above is read:用简单的英语,上面的内容是:

If skyB is false OR is day one, print "example test"如果 skyB 为假或为第一天,则打印“示例测试”

If either part of the expression fails to evaluate to true (ie if skyB is true and day variable is any value other than one), it will not print anything.如果表达式的任何一部分未能计算为真(即如果 skyB 为真并且 day 变量是除 1 以外的任何值),它将不会打印任何内容。

UPDATE :更新

Even if this answer has been accepted and upvoted, I would like to include some information about boolean expressions for the benefit of new developers that might be struggling with this concept.即使这个答案已被接受和投票,我还是想包含一些有关 boolean 表达式的信息,以帮助可能在这个概念上苦苦挣扎的新开发人员。

What is a Boolean Expression?什么是 Boolean 表达式?

According to Oracle documentation , " A Boolean expression is a logical statement that is either TRUE or FALSE. " These expressions take a few different forms. For example,根据Oracle 文档,“ Boolean 表达式是逻辑语句,要么为 TRUE 要么为 FALSE。 ”这些表达式采用几个不同的 forms。例如,

  • Boolean literals Boolean 文字
if (true) {...}

Although there is no real reason for such if-statement, the above is a valid expression.虽然这样的 if 语句没有真正的原因,但上面是一个有效的表达式。 "If true, do the stuff inside the curly braces." “如果为真,则执行花括号内的操作。” This means that the stuff inside will always execute (making it the same as not having the "if". However, this is the basis for understanding the rest.这意味着里面的东西会一直执行(让它和没有“如果”一样)。但是,这是理解rest的基础。

  • Boolean variables Boolean 个变量
boolean isBlue = false;
if (isBlue) {...}

In the above case, the Boolean expression is the variable itself.在上面的例子中,Boolean 表达式就是变量本身。 The expectation is that, if the variable value evaluates to true , the stuff inside the curly braces will be executed.期望是,如果变量值的计算结果为true ,则将执行花括号内的内容。 Since the value is false, it won't be executed.由于该值为 false,因此不会执行。 The above "if" is the more accepted short form of上面的“如果”是更容易接受的缩写形式

boolean isBlue = false;
if (isBlue == true) {...}

So, since the left hand (variable) value of this evaluation is false and the right side is true, the complete expression evaluates to false.因此,由于此评估的左侧(变量)值为 false 而右侧为 true,因此完整表达式的评估结果为 false。 Therefore, the stuff inside curly braces won't be executed (false does not equals true).因此,花括号内的内容不会被执行(false 不等于 true)。 However然而

boolean isBlue = false;
if(!isBlue == true){...}

Negating a boolean variable toggles it's value.否定 boolean 变量会切换它的值。 In this case, the isBlue variable holds the value of false .在这种情况下, isBlue变量的值为false The "not" operator toggles its value to true . “not”运算符将其值切换为true Since true == true evaluates to true (true equals true is a true expression), the stuff inside curly braces is executed.由于true == true计算结果为 true(true 等于 true 是一个 true 表达式),因此执行大括号内的内容。

  • Functions that yield a boolean value产生 boolean 值的函数
public boolean isSkyBlue() {
  return true; // sorry for the over simplification
}
...
if (isSkyBlue()) {...}

This is nothing more than an advance form of what I have shown already.这只不过是我已经展示的内容的高级形式。 In this case, the method "computes" the boolean value to be returned.在这种情况下,该方法“计算”要返回的 boolean 值。 Since boolean values are expressions onto themselves, it is valid to insert a method call such as the one illustrated above inside the parentheses.由于 boolean 值是自身的表达式,因此在括号内插入方法调用(如上图所示)是有效的。 The stuff inside curly braces will execute so long the function returns true .只要 function 返回true ,花括号内的内容就会执行。

  • Comparison operations比较操作

I included the following example already:我已经包含了以下示例:

int x = 5;
int y = 3;

if (x > y) {
  System.out.println("x is greater than y");
}

Any type of comparison operation will yield true or false.任何类型的比较操作都会产生 true 或 false。 Therefore, these comparison operations are boolean expressions.因此,这些比较操作是boolean个表达式。 This also includes the result of overridden Object#equals(Object) method.这也包括覆盖Object#equals(Object)方法的结果。 For example:例如:

String s = "Hello";
if (s.equals("Hello")) {...}

As a side note, objects in Java should be compared using an overridden equals() method, and not using the double-equals operator (unless you are only interested in comparing the object references (address) to be equal.作为旁注,应该使用覆盖的equals()方法比较 Java 中的对象,而不是使用双等号运算符(除非您只对比较 object 引用(地址)是否相等感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM