简体   繁体   English

在 Java 中执行 If 和 Else-if 语句之间的代码

[英]Execute Code Between If and Else-if Statement in Java

I got this code:我得到了这个代码:

if (conditionX) {

                    doX();
                }



else if (conditionY) {
                    boolean isSomethingTrue = findOutIfSomethingIsTrue();
                    if (anotherConditionIsTrue) {
                        doY();
                    }
                }

XYZPojo xyzPOJO = getXYZPOJO();    

else if (xyzPOJO.getValueX().contains("xyz")) {  // <- eclipse wants me to delete this 'else' statement

                    doXYZ();

                }

When I run this code eclipse complains that "Syntax error on token "else", delete this token" .当我运行此代码时,eclipse 会抱怨"Syntax error on token "else", delete this token" I understand this is because I have the code:我明白这是因为我有代码:

boolean conditionXYZ = checkIfXYZIsTrue();

between both else if statements.else if语句之间。 But I cannot check the value of conditionXYZ unless I set the value of conditionXYZ in between both else if statements.但是我无法检查conditionXYZ的值,除非我在else if语句之间设置了conditionXYZ的值。

The only other similar question I found on SO is this one and it suggests to use two if statements.我在 SO 上发现的唯一另一个类似问题是这个问题,它建议使用两个 if 语句。 The problem is I don't want conditionXYZ to be evaluated if conditionY is true.问题是我不想conditionXYZ如果要评估conditionY是真实的。

My other option is to use two if statements and add a return statement for each if statement.我的另一个选择是使用两个if statements并为每个 if 语句添加一个 return 语句。 But I do not find this solution to be elegant in terms of code design.但我认为这个解决方案在代码设计方面并不优雅。

My third option is to checkIfXYZIsTrue() before executing any of the if statements but this is not efficient because if conditionY is true we don't need to check conditionXYZ .我的第三个选择是在执行任何 if 语句之前checkIfXYZIsTrue() ,但这效率不高,因为如果conditionY为真,我们不需要检查conditionXYZ

Any ideas?有任何想法吗?

Thanks in advance提前致谢

Java syntax doesn't allow you to have statements between chains of if/else (that's not in their respective blocks). Java 语法不允许在 if/else 链之间使用语句(不在它们各自的块中)。

You can execute your third condition in the else if condition:您可以在else if条件中执行您的第三个条件:

if (conditionX) {
    doX();
} else if (conditionY) {
    boolean isSomethingTrue = findOutIfSomethingIsTrue();
    if (anotherConditionIsTrue) {
        doY();
    }
} else {
    XYZPojo xyzPOJO = getXYZPOJO();
    if (xyzPOJO.getValueX().contains("xyz")) {
        doXYZ();
    }
    //reuse xyzPOJO
}

Alternatively, the last else can still look like:或者,最后一个else仍然可以看起来像:

} else if (getXYZPOJO().getValueX().contains("xyz")){
    doXYZ();
}

I know this is pretty old, but seeing as no one gave the answer I would have, I figured I would post my answer incase anyone else finds their way here looking for options.我知道这已经很老了,但看到没有人给出我想要的答案,我想我会发布我的答案,以防其他人在这里找到自己的方式寻找选项。

The problem is I don't want conditionXYZ to be evaluated if conditionY is true.问题是我不希望在 conditionY 为真时对 conditionXYZ 进行评估。

You could use a ternary operator to check that conditionY is false before evaluating conditionXYZ above the if/else statements.在评估 if/else 语句上方的 conditionXYZ 之前,您可以使用三元运算符来检查 conditionY 是否为假。

//Sets xyzPOJO to getXYZPOJO() if conditionY isn't true, otherwise set xyzPOJO to null
XYZPojo xyzPOJO = conditionY!=true ? getXYZPOJO() : null;

if (conditionX) {
    doX();
}
else if (conditionY) {
    boolean isSomethingTrue = findOutIfSomethingIsTrue();
    if (anotherConditionIsTrue) {
        doY();
    }
}
else if (xyzPOJO.getValueX().contains("xyz")) {
    doXYZ();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM