如何在一个方法中返回两个in-line if-else语句?JAVA

如何在一个方法中返回两个in-line if-else语句?JAVA,java,methods,return,Java,Methods,Return,我试图从blankEnd方法返回两个结果。我想测试this.end1是否为0,还想测试this.end2是否为0。如何组合这两个返回语句?谢谢 public boolean blankEnd() { return this.end1 == 0 ? true : false; return this.end2 == 0 ? true : false; } 只要使用&& 编辑:事实上,如果您只想返回true/false,则不需要条件语句: 只需使用: return (this.end1 == 0

我试图从blankEnd方法返回两个结果。我想测试this.end1是否为0,还想测试this.end2是否为0。如何组合这两个返回语句?谢谢

public boolean blankEnd() {
return this.end1 == 0 ? true : false;
return this.end2 == 0 ? true : false;
}
只要使用&&

编辑:事实上,如果您只想返回true/false,则不需要条件语句:

只需使用:

return (this.end1 == 0 && this.end2 == 0);
您有四个案例:

this.end1  this.end2
       0          0
       0      not 0
   not 0          0
   not 0      not 0
如果要返回单个布尔值,则需要决定要为每个组合返回什么。然后可以编写适当的逻辑表达式。因为有四行,每行有两个可能的返回值true或false,所以可以在这里定义16个不同的函数。例如,如果要在end1或end2为true时返回true,则可以使用:

return this.end1 == 0 || this.end2 == 0;
如果要返回两个布尔值,可以返回一个数组:

public boolean[] blankEnd() {
    return new boolean[] { this.end1 == 0, this.end2 == 0};
}
ApacheCommons有一个数据结构,可以在多种情况下使用,您可以查看


如果您有任何问题,请告诉我

如果不想返回布尔值数组,则必须返回单个值。您可以根据变量end1和end2决定返回什么,如

public boolean blankEnd() {
    return (this.end1 || this.end2) ? true: false; // Returns true if any variable is 0
}


您可以使用逻辑and

return (this.end1 == 0 && this.end1 == this.end2);
或者,您可以通过应用


一个布尔数组?一个方法只能返回一个值,因此如果需要多个布尔值,请返回它们的数组。使用单个布尔值返回值,如果this.end1==0,this.end2!=0?+1作为您的解释,而不仅仅是给出代码答案
public boolean blankEnd() {
    return (this.end1 && this.end2) ? true: false; // Returns true if both variables are 0
}
return (this.end1 == 0 && this.end1 == this.end2);
return !(this.end1 != 0 || this.end1 != this.end2);