Java 当达到警告时,Intellij条件始终为假
我有下面的代码 我有一个枚举和一个BigInteger变量。请查找使用的if条件Java 当达到警告时,Intellij条件始终为假,java,intellij-idea,Java,Intellij Idea,我有下面的代码 我有一个枚举和一个BigInteger变量。请查找使用的if条件 public enum EnumCode { Open, Closed, None } private boolean TEST(final ConstraintValidatorContext constraintValidatorContext, final BigInteger amount, final EnumCode enumCode) {
public enum EnumCode {
Open,
Closed,
None
}
private boolean TEST(final ConstraintValidatorContext constraintValidatorContext, final BigInteger amount, final EnumCode enumCode) {
if (enumCode == EnumCode.Closed && null == amount) {
//error message1
return false;
}
if (enumCode != EnumCode.Closed && null != amount) {
//error message2
return false;
}
if (null == enumCode && null != amount) { //**Condition is always false when reached**
//error message3
return false;
}
return true;
}
如何修复警告
if (enumCode == EnumCode.Closed && null == amount) {
return false;
}
if (enumCode != EnumCode.Closed && null != amount) {
return false;
}
如果
enumCode==null
,则enumCode!=EnumCode.Closed
。因此,这:
if (null == enumCode && null != amount) {
return false;
}
如果前面的条件为真,则永远不会为真
要解决此问题,请删除final if语句,因为它是多余的
另外,您可以只使用一个if语句来编写它:
if ((enumCode == EnumCode.Closed) == (null == amount)) {
return false;
}
如果
enumCode==null
,则enumCode!=EnumCode.Closed
。因此,这:
if (null == enumCode && null != amount) {
return false;
}
如果前面的条件为真,则永远不会为真
要解决此问题,请删除final if语句,因为它是多余的
另外,您可以只使用一个if语句来编写它:
if ((enumCode == EnumCode.Closed) == (null == amount)) {
return false;
}
如果
enumCode
为null
,则条件enumCode!=EnumCode.Closed
为true。
因此,第二个if
条件包括第三个条件
您应该检查它是否与EnumCode.Closed
不同,以及它是否为null
if (enumCode == EnumCode.Closed && null == amount) {
return false;
}
if (enumCode != null && enumCode != EnumCode.Closed && null != amount) {
return false;
}
if (null == enumCode && null != amount) {
return false;
}
如果
enumCode
为null
,则条件enumCode!=EnumCode.Closed
为true。
因此,第二个if
条件包括第三个条件
您应该检查它是否与EnumCode.Closed
不同,以及它是否为null
if (enumCode == EnumCode.Closed && null == amount) {
return false;
}
if (enumCode != null && enumCode != EnumCode.Closed && null != amount) {
return false;
}
if (null == enumCode && null != amount) {
return false;
}
从逻辑上讲,一旦达到第三个if条件,变量量将不等于null,因此null!=金额将导致错误。其次,null==enumCode也将导致false,因为您已经在第二个if语句中验证了它。此外,在您的第二个if语句中,当说null!=枚举代码。已关闭;条件为true的可能值包括{null,open,none}。这意味着您不需要使用null==enumCode再次检查它,因为它无论如何都是false。根据布尔代数,false&&false是false。要解决这个问题,只需删除第三个if,因为它们包含在以前的if中
public enum EnumCode {
Open,
Closed,
None
}
private boolean TEST(final ConstraintValidatorContext constraintValidatorContext, final BigInteger amount, final EnumCode enumCode) {
if (enumCode == EnumCode.Closed && null == amount) {
//error message1
return false;
}
if (enumCode != EnumCode.Closed && null != amount) {
//error message2
return false;
}
return true;
}
从逻辑上讲,一旦达到第三个if条件,变量量将不等于null,因此null!=金额将导致错误。其次,null==enumCode也将导致false,因为您已经在第二个if语句中验证了它。此外,在您的第二个if语句中,当说null!=枚举代码。已关闭;条件为true的可能值包括{null,open,none}。这意味着您不需要使用null==enumCode再次检查它,因为它无论如何都是false。根据布尔代数,false&&false是false。要解决这个问题,只需删除第三个if,因为它们包含在以前的if中
public enum EnumCode {
Open,
Closed,
None
}
private boolean TEST(final ConstraintValidatorContext constraintValidatorContext, final BigInteger amount, final EnumCode enumCode) {
if (enumCode == EnumCode.Closed && null == amount) {
//error message1
return false;
}
if (enumCode != EnumCode.Closed && null != amount) {
//error message2
return false;
}
return true;
}
“如何修复警告?”删除该if语句。
enumCode!=如果EnumCode==null
,则EnumCode.Closed
为真。第二个if条件包括第三个ifcondition@jhamon:谢谢。看起来我犯了一个非常愚蠢的错误当你达到第三个if条件时,金额将不等于null所以null!=金额将导致错误。其次,null==enumCode也将导致false,因为您已经在第二个if语句中验证了它。在第二个if语句中,当说null时枚举代码。已关闭;[条件为true的可能值包括{null,open,none}。这意味着您不需要再次检查它null==enumCode,因为它将为false。根据布尔代数,false&&false为false。您只需删除第三个if,因为它们包含在以前的if中。“如何修复警告?”删除该if语句。enumCode!=enumCode.Closed
如果enumCode==null
,则为true。第二个if条件包括第三个ifcondition@jhamon:谢谢。看起来我犯了一个非常愚蠢的错误。当你达到第三个if条件时,amount将不等于null,因此null!=amount将导致false。其次,null==枚举代码将o结果为false,因为您已经在第二个if语句中验证了它。在第二个if语句中,当说null!=EnumCode.Closed时,[条件为true的可能值包括{null,open,none}。这意味着您不需要再次检查它null==enumCode,因为它将为false。根据布尔代数,false&&false为false。您只需删除第三个if,因为它们包含在以前的if中。