簡體   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