合并多个if语句

问题描述 投票:0回答:3

我有以下代码依赖于多个不同的if语句。我正在制作Android的Cribbage分数计数应用程序。显示的代码仅适用于蓝色团队,但是我最终为蓝色团队使用的代码也将用于红色和绿色团队。我现在所拥有的有效,但是体积很大。我是Java的新手,并希望获得一些有关将代码合并为更易于阅读和维护的最佳实践的指导。

    if(blueTeamScore >= ENDGAMESCORE) {
        String baseVictoryText = "Blue Team has won!";
        TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);

        if(hasThreePlayer == false) {
            winningTeamTextView.setText(baseVictoryText);
            if (hasSkunk == true) {
                if (redTeamScore <= 90) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Red Team got Skunked");
                }
            }
        }
        if(hasThreePlayer == true) {
            winningTeamTextView.setText(baseVictoryText);
            if (hasSkunk == true) {
                if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Red and Green Teams got Skunked");
                }
                if ((redTeamScore <= 90) && (greenTeamScore >= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Red Team got Skunked");
                }
                if ((redTeamScore >= 90) && (greenTeamScore <= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Green Team got Skunked");
                }
            }
        }
    }

我知道布尔变量不能在switch语句中使用(太糟糕了,但是还有其他类似的东西可以帮助我集中代码吗?

java android if-statement switch-statement logic
3个回答
1
投票

我仍然认为自己还很新,但这就是我会写的。我对此进行了几次编辑。

if (hasSkunk) {
    if (!hasThreePlayer) {
        if (redTeamScore<=90) {
            baseVictoryText += "\nAnd Red Team got Skunked";
        }
    } else {
        if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {
            baseVictoryText += "\nAnd Red and Green Teams got Skunked";
        } else if (redTeamScore <= 90) {
            baseVictoryText += "\nAnd Red Team got Skunked";
        } else if (greenTeamScore <= 90) {
            baseVictoryText += "\nAnd Green Team got Skunked";
        }
    }
}
winningTeamTextView.setText(baseVictoryText);

假设您不介意更改baseVictoryText(不是常数)。否则,将其分配给其他内容并使用

[不假设'hasSunkunk'意味着至少有一个团队拥有<90,否则,如果最后一个则变成其他...

'Else if'在这里在理论上由于性能而更好-仅对ifs进行评估,直到一个为真为止。这不是问题,但是如果其中一个条件是使用繁重方法的结果...

像这样的一堆其他的Ifs,非常类似于带有断点的切换。


1
投票

您可以尝试使用三元运算符?:。上面代码中的示例我只是简化了一部分。您可以在其他地方尝试类似的操作

        if(!hasThreePlayer) {
            winningTeamTextView.setText(hasSkunk&&redTeamScore<=90 ? baseVictoryText + "\nAnd Red Team got Skunked" : baseVictoryText);
        }
        else {
            winningTeamTextView.setText(baseVictoryText);
            if (hasSkunk) {
                if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Red and Green Teams got Skunked");
                }
                if ((redTeamScore <= 90) && (greenTeamScore >= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Red Team got Skunked");
                }
                if ((redTeamScore >= 90) && (greenTeamScore <= 90)) {
                    winningTeamTextView.setText(baseVictoryText + "\nAnd Green Team got Skunked");
                }
            }
        }

0
投票

干净一点,只将textField放在一个地方,不需要==布尔值,如果应该分支,因为如果一个成功,另一个将失败。初始响应略有变化。

if(blueTeamScore >= ENDGAMESCORE) {
    String victoryText = "Blue Team has won!";

    if(!hasThreePlayer && hasSkunk && redTeamScore <= 90) {
        victoryText += "\nAnd Red Team got Skunked");

    }else if(hasThreePlayer && hasSkunk) {
       if (redTeamScore <= 90 && greenTeamScore <= 90) {
          victoryText +=  "\nAnd Red and Green Teams got Skunked";
       }else if (redTeamScore <= 90) {
          victoryText +=  "\nAnd Red Team got Skunked";
       }else if (greenTeamScore <= 90) {
          victoryText += "\nAnd Green Team got Skunked";
       }
    }

    TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);
    //maybe check for null textfield here?
    winningTeamTextView.setText(victoryText);
}

但是我会分解成一个函数

 if(blueTeamScore >= ENDGAMESCORE) {
    String victoryText = "Blue Team has won!";
    if(hasSkunk){
        victoryText += getSkunkText(hasThreePlayer, redTeamScore, greenTeamScore)
    }

    TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);
    //maybe check for null textfield here?
    winningTeamTextView.setText(victoryText);
}

功能:

private String getSkunkText(boolean hasThreePlayer, int redTeamScore, int greenTeamScore){
    String skunkText  = ""
    if(!hasThreePlayer) {
        skunkText = redTeamScore <= 90 ? "\nAnd Red Team got Skunked") : skunkText;
    }else if (redTeamScore <= 90 && greenTeamScore <= 90) {
        skunkText =  "\nAnd Red and Green Teams got Skunked";
    }else if ( redTeamScore <= 90) {
        skunkText =  "\nAnd Red Team got Skunked";
    }else if (greenTeamScore <= 90) {
        skunkText = "\nAnd Green Team got Skunked";
    }
    return skunkText; 
}
© www.soinside.com 2019 - 2024. All rights reserved.