繁体   English   中英

Java中是否可能有一个不使用关系运算符的if语句?

[英]Is it possible to have an if statement in Java that doesn't make use of relational operators?

这不是特定的代码,我很好奇。 关系运算符,例如==<>>=<=!=

当然:

if (processing)
{
    // enter if the boolean processing is true
}

假设您有一个返回布尔值的函数

  public boolean isTrue()
  {
    //Some code
    //return true or false

  }

然后,“ if”语句将如下所示:

  if(isTrue())
     //Do something
  else
    //Do something else

好吧,实际上,if语句只知道它是真还是假。
但是,如果是整数,则如果内括号正确,则返回true。
喜欢:

int n=10;  
if(n==10){}

括号内的值将返回true,因为它是正确的。 如果将括号更改为n>10则返回false

字符串的条件不同。

是:

if (x) {
    ...
}

要么

if (!x) {
    ...
}

其中x是布尔值或任何返回布尔值的值。

哎呀,发疯:

boolean x = false, y = false;

if (x = y = x = !y) {

}

(请注意,我们使用=不是==在这里-你可能很少需要做这样的事情,如果有的话)。

是。 一般来说, if语句的语法是if (cond) { xxx(); } if (cond) { xxx(); }其中cond任何计算结果为boolean表达式

这意味着以下所有内容均有效:

if (true)           // Literal value true
if (m.find())       // m is a Matcher; .find() returns true if match
if (3 <= 3)         // operator
if (a && b)         // a and b are expressions evaluating to a boolean;
                    // && is the logical "and" operator

等等。

暂无
暂无

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

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