【问题标题】:How to check a boolean method return value in java如何在java中检查布尔方法的返回值
【发布时间】:2016-09-03 15:15:33
【问题描述】:

我对 Java 很陌生,我尝试做的事情可能看起来很奇怪,但我需要对 Java 的工作原理有所了解,而不是实际完成一个设定的结果。

如果我有一个布尔方法,例如:

public class doThings
{

    // imagine the intial value of this variable is set
    // and or modified using getters and setters between the declaration
    // and the following method
    private boolean boolVar;


    public boolean doSomething()
    {
        if(boolVar == true)
        {
            //does things and then returns true
            return true;
        }
        else
        {
            return false;
        }
    }
}

我想在另一个类中调用这个方法......

public class testDoThings
{

    doThings someObject = new doThings;
    public static void main(String[] args)
    { 
        someObject.doSomething()
    }
} 

我如何检查(在 testDoThings 类中)以查看该方法是返回 true 还是返回 false 并相应地打印类似这样的消息...

public class testDoThings
{

    doThings someObject = new doThings;
    public static void main(String[] args)
    {

        someObject.doSomething()
        if (doSomething() returns true) // yes I am aware this is not
                                        //valid
        { 
            // print a statment
        }
        else if (doSomething() returns false) // yes once again not valid
        {
            // print a different statement           
        }
    }
} 

我知道您可以将这些消息放在包含该方法的类中,但是如果我想要不同的消息,具体取决于调用方法的位置和调用的对象,那么将内容放在原始类方法中并不总是可行上班。

如果我在这里完全偏离轨道,请告诉我,如果有人可以向我解释这一点,那就太好了!

【问题讨论】:

    标签: java class return boolean


    【解决方案1】:

    你可以这样试试:

    if (someObject.doSomething()) //So if your function returns true then the below statement will be executed
    { 
       // print a statment
    }
    else //This will check for the false condition of your function
    {
       // print a different statement           
    }
    

    【讨论】:

    • if(someObject.doSomething() == true){ }else{ } 相同。更多运营商请查看本站:link
    【解决方案2】:

    你可以试试这样的:

    if(someObject.doSomething()){
        System.out.print("foo1");
    }
    
    else{
        System.out.print("foo2");
    }
    

    【讨论】:

      【解决方案3】:

      这是一种方法:

      if(someObject.doSomething() == true){
          System.out.print("foo1");
      }
      
      else{
          System.out.print("foo2");
      }
      

      【讨论】:

        【解决方案4】:

        通常您使用== 运算符比较两件事:if (x == y) ... 所以我们有:

        if ( someObject.doSomething() == true ) {
            //statements
        } else {
            //statement for the case where result of method call is false
        }
        

        顺便说一句,你可以简单地写if(x),而不是if(x == true)

        【讨论】:

          【解决方案5】:

          if、while、do...等条件结构接收一个布尔值,因此没有必要输入“boolVar == true”。只需执行“if (boolVar)”就足够了。至于 doSomething 方法中的示例,只需执行“return boolVar;”会做这项工作,不需要任何如果,除非你假装在上面做更多的事情。

          检查函数返回值的工作方式相同。我的意思是,变量也有值和函数,唯一的区别是变量保存一个值,而函数计算或生成一个值。所以,在你的代码中:

          public class testDoThings {
              public void check() {
          
                 doThings someObject = new doThings();
          
                  boolean flag = sameObject.doSomething();
                  if (flag) {
          
                      // print a statment
          
                   } else {
                     //If you want to check falsehood, !flag would do.
                                       // Notice the "!" sign before 
                                       // the flag variable?
                                       // It is a NOT sign, so
                                       // NOT true = false
                                       // NOT false = true
                                       // The if will execute its code 
                                       // When the result value is true
                                       // So when the function return val is                           
                                       // false.
          
                      // print a different statement           
                  }
              }
          }
          

          我希望这个解释足够清楚。

          【讨论】:

          • 为什么需要“else..if”?由于条件(方法调用)是布尔值,一个简单的 else 就足够了。此外,您可能不想调用同一个方法两次,只是为了检查返回。
          • 这可能很糟糕,因为它可能导致 doSomething 实际执行两次。
          • 我同意。到两个 cmets。我是专业的编码员。我只是试图填补这些漏洞。一切都可以改进。让我解决它。
          • 我不明白人们怎么能对这里的新手来说更具解释性和最容易理解的内容投反对票。但是,嘿,没关系。如果您喜欢它,请投反对票。
          • 在您编辑帖子之前,他们可能会否决它。但是无论如何,就目前而言,您的代码仍然无法编译,因为您必须将其包装在一个方法中。如果你修复它,你的 cmets 也会得到支持。
          猜你喜欢
          • 2011-11-28
          • 2016-01-25
          • 1970-01-01
          • 2013-03-28
          • 2013-01-04
          • 1970-01-01
          • 2012-11-03
          • 1970-01-01
          • 2015-02-14
          相关资源
          最近更新 更多