如何在java中检查布尔方法返回值

如何在java中检查布尔方法返回值,java,class,return,boolean,Java,Class,Return,Boolean,我对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 fol

我对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           
        }
    }
} 
我知道您可以将这些消息放在包含该方法的类中,但是如果我需要不同的消息,这取决于调用该方法的位置和调用的对象,那么将消息放在原始类方法中并不总是可行的

如果我完全偏离了轨道,请让我知道,如果有人能向我解释一下,那就太好了

您可以这样尝试:

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()){
    System.out.print("foo1");
}

else{
    System.out.print("foo2");
}

条件结构,如if、while、do…,等接收布尔值,因此无需输入“boolVar==true”。仅仅做“如果(布尔瓦尔)”就足够了。至于你在doSomething方法中的例子,只要做“return boolVar;”就可以完成这项工作,而不需要任何if,除非你假装在上面做更多的事情

检查函数返回值的方法与此相同。我的意思是,变量有值,函数也有值,唯一的区别是变量有值,而函数计算或生成值。因此,在您的代码中:

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           
        }
    }
}
我希望这个解释足够清楚。

这里有一个方法:

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

else{
    System.out.print("foo2");
}

通常使用
==
运算符比较两件事:
如果(x==y).
那么我们有:

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

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

,您可以尝试以下方法:

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()){
    System.out.print("foo1");
}

else{
    System.out.print("foo2");
}

这与if(someObject.doSomething()==true){}else{}
相同。欲了解更多运营商,请访问此网站:为什么需要“else..if”?因为条件(方法调用)是布尔的,所以简单的else就足够了。另外,您可能不想为了检查返回而调用同一个方法两次。这可能会导致
doSomething
实际执行两次。我同意。对这两个评论都不满意。我是专业的编码员。我只是想填补这些漏洞。一切都可以改进。让我来解决这个问题。我不明白人们怎么能否决对新手来说更具解释性和最容易理解的建议。但是,嘿,没关系。如果你喜欢,就投否决票。在你编辑你的帖子之前,他们很可能投了否决票。但无论如何,就目前而言,您的代码仍然无法编译,因为您必须将其包装在一个方法中。如果你把它和你的评论都修正了,你会得到一张赞成票。