Java 使用 || 进行返回。

huangapple go评论56阅读模式
英文:

Java return using ||

问题

这段代码的逻辑是怎样的?

boolean left = printLevel(root.left, level - 1);
boolean right = printLevel(root.right, level - 1);

return left || right;
英文:

How does this code's logic work?

boolean left = printLevel(root.left, level - 1);
boolean right = printLevel(root.right, level - 1);

return left || right;

答案1

得分: 1

它返回您表达式的boolean结果。

例如,return false || true;首先会计算“false || true”,然后返回该计算的结果,即根据真值表,为true

英文:

It returns the boolean result of your expression.

For example, return false || true; would first evaluate "false || true", and then it will return the result of that evaluation, which is true, according to the truth table.

答案2

得分: 0

方法 printLevel 以布尔格式返回结果:基于提供的输入,可能是 truefalse

方法 printLevel 的执行结果存储在你提供的示例代码的第一行和第二行的变量 leftright 中。

现在,在第三行中,|| 执行逻辑 OR 运算。其工作方式如下:

true  || false --> true
true  || true  --> true
false || true  --> true
false || false --> false
英文:

The method printLevel returns result in boolean format: true or false based on the input provided.

The results of the method printLevel execution is stored to the variables left and right available in the 1st and 2nd line of the sample code you provided.

Now, || is doing logical OR operation in the 3rd line. Which works following way:

true  || false --> true
true  || true  --> true
false || true  --> true
false || false --> false

huangapple
  • 本文由 发表于 2020年10月10日 13:26:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64290283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定