【问题标题】:How do I check if a method returns true or false in an IF statement in Java?如何检查方法在 Java 的 IF 语句中返回 true 还是 false?
【发布时间】:2012-11-14 17:41:00
【问题描述】:

假设我有一个布尔方法,它使用 if 语句来检查返回类型应该是 true 还是 false

public boolean isValid() {
   boolean check;
   int number = 5;

   if (number > 4){
      check = true;
   } else {
      check = false;
   }

 return check;

现在,我想在 不同 方法中将此方法用作 if 语句的参数:

if(isValid == true)  // <-- this is where I'm not sure
   //stop and go back to the beginning of the program
else
   //continue on with the program

所以基本上我要问的是,我如何检查 if 语句的参数中的布尔方法的返回类型是什么?非常感谢您的回答。

【问题讨论】:

  • 怎么样 - if (ifValid())。如果您注意到它,您的方法将始终返回 true
  • 是的,你的方向是正确的...... :)
  • 有点离题,但偶然发现这个问题让我想起了我作为一名程序员已经走了多远。很棒的感觉。

标签: java methods if-statement boolean


【解决方案1】:

因为它是一个方法,调用它你应该在之后使用括号,这样你的代码就会变成:

if(isValid()) {
    // something
} else {
    //something else
}

【讨论】:

  • 这里不需要加上“== true”。
【解决方案2】:
public boolean isValid() {
   int number = 5;
   return number > 4;
}

if (isValid()) {
    ...
} else {
    ...
}

【讨论】:

    【解决方案3】:
    if (isValid()) {
       // do something when the method returned true
    } else {
       // do something else when the method returned false
    }
    

    【讨论】:

      【解决方案4】:

      您应该能够在 IF 条件内调用该函数,因此:

      if (isValid()) {
      
      }else {
      
      }
      

      由于isValid() 返回boolean,因此将立即评估条件。我听说在你测试你的条件之前创建一个本地变量是更好的形式。

       boolean tempBoo = isValid();
      
       if (tempBoo) {
      
       }else {
      
       }
      

      【讨论】:

        【解决方案5】:

        你可以使用:

        if(isValid()){
             //do something....
        }
        else
        {
            //do something....
        }
        

        【讨论】:

          【解决方案6】:

          - If 声明仅接受 boolean 值。

          public boolean isValid() {
          
             boolean check = false;   // always intialize the local variable
             int number = 5;
          
             if (number > 4){
                check = true;
             } else {
                check = false;
             }
          
           return check;
          
          }
          
          
          if(isValid()){
          
              // Do something if its true
          }else{
          
              // Do something if its false
          }
          

          【讨论】:

          • 很好的答案将充分发挥作用并初始化变量,有时当大脑工作缓慢时,这些答案会让一天...
          【解决方案7】:
          public boolean isValid() {
             boolean check;
             int number = 5;
          
             if (number > 4){
                check = true;
             } else {
                check = false;
             }
          
           return check;
          

          如何在没有布尔检查的情况下完成整个方法?

          那么如何摆脱.. check = true, check = false, return check 的东西?

          【讨论】:

          • 你可以只返回评估本身,如'return number > 4;' 这将把你的方法压缩成更少的行。
          猜你喜欢
          • 1970-01-01
          • 2017-08-10
          • 2020-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-09
          • 2016-01-04
          • 1970-01-01
          相关资源
          最近更新 更多