合并多个 if 语句

我有以下代码,它依赖于多个不同的 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 语句中使用(太糟糕了),但是还有其他类似的东西可以帮助集中我的代码吗?


呼唤远方
浏览 98回答 3
3回答

慕容森

我仍然认为自己相当新,但这就是我会写的内容。我编辑了几次。if (hasSkunk) {&nbsp; &nbsp; if (!hasThreePlayer) {&nbsp; &nbsp; &nbsp; &nbsp; if (redTeamScore<=90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; baseVictoryText += "\nAnd Red Team got Skunked";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; baseVictoryText += "\nAnd Red and Green Teams got Skunked";&nbsp; &nbsp; &nbsp; &nbsp; } else if (redTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; baseVictoryText += "\nAnd Red Team got Skunked";&nbsp; &nbsp; &nbsp; &nbsp; } else if (greenTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; baseVictoryText += "\nAnd Green Team got Skunked";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}winningTeamTextView.setText(baseVictoryText);假设您不介意更改 baseVictoryText (不是常量)。否则将其分配给其他东西并使用它不假设“hasSkunk”意味着至少有一支球队的得分<90,否则最后一个 else if 会变成 else ...由于性能原因,“Else if”在理论上会更好 - 只有在其中一个为真时才会评估 if。这在这里不是问题,但如果其中一个条件是某种重型方法的结果......像这样的一堆 Else If 的工作方式非常类似于带有中断的 switch。

撒科打诨

您可以尝试三元运算符 ?: 。上面代码中的示例我只是简化了某些部分。你可以尝试其他类似的东西&nbsp; &nbsp; &nbsp; &nbsp; if(!hasThreePlayer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; winningTeamTextView.setText(hasSkunk&&redTeamScore<=90 ? baseVictoryText + "\nAnd Red Team got Skunked" : baseVictoryText);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; winningTeamTextView.setText(baseVictoryText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hasSkunk) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((redTeamScore <= 90) && (greenTeamScore <= 90)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; winningTeamTextView.setText(baseVictoryText + "\nAnd Red and Green Teams got Skunked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((redTeamScore <= 90) && (greenTeamScore >= 90)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; winningTeamTextView.setText(baseVictoryText + "\nAnd Red Team got Skunked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((redTeamScore >= 90) && (greenTeamScore <= 90)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; winningTeamTextView.setText(baseVictoryText + "\nAnd Green Team got Skunked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

慕勒3428872

更干净一点,因为只在一个地方设置 textField,不需要 == 一个布尔值,if 应该分支,因为如果一个成功,另一个就会失败。与初始响应略有不同。if(blueTeamScore >= ENDGAMESCORE) {&nbsp; &nbsp; String victoryText = "Blue Team has won!";&nbsp; &nbsp; if(!hasThreePlayer && hasSkunk && redTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; victoryText += "\nAnd Red Team got Skunked");&nbsp; &nbsp; }else if(hasThreePlayer && hasSkunk) {&nbsp; &nbsp; &nbsp; &nbsp;if (redTeamScore <= 90 && greenTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; victoryText +=&nbsp; "\nAnd Red and Green Teams got Skunked";&nbsp; &nbsp; &nbsp; &nbsp;}else if (redTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; victoryText +=&nbsp; "\nAnd Red Team got Skunked";&nbsp; &nbsp; &nbsp; &nbsp;}else if (greenTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; victoryText += "\nAnd Green Team got Skunked";&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);&nbsp; &nbsp; //maybe check for null textfield here?&nbsp; &nbsp; winningTeamTextView.setText(victoryText);}但我会分解成一个函数&nbsp;if(blueTeamScore >= ENDGAMESCORE) {&nbsp; &nbsp; String victoryText = "Blue Team has won!";&nbsp; &nbsp; if(hasSkunk){&nbsp; &nbsp; &nbsp; &nbsp; victoryText += getSkunkText(hasThreePlayer, redTeamScore, greenTeamScore)&nbsp; &nbsp; }&nbsp; &nbsp; TextView winningTeamTextView = (TextView) findViewById(R.id.winningTeam);&nbsp; &nbsp; //maybe check for null textfield here?&nbsp; &nbsp; winningTeamTextView.setText(victoryText);}功能:private String getSkunkText(boolean hasThreePlayer, int redTeamScore, int greenTeamScore){&nbsp; &nbsp; String skunkText&nbsp; = ""&nbsp; &nbsp; if(!hasThreePlayer) {&nbsp; &nbsp; &nbsp; &nbsp; skunkText = redTeamScore <= 90 ? "\nAnd Red Team got Skunked") : skunkText;&nbsp; &nbsp; }else if (redTeamScore <= 90 && greenTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; skunkText =&nbsp; "\nAnd Red and Green Teams got Skunked";&nbsp; &nbsp; }else if ( redTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; skunkText =&nbsp; "\nAnd Red Team got Skunked";&nbsp; &nbsp; }else if (greenTeamScore <= 90) {&nbsp; &nbsp; &nbsp; &nbsp; skunkText = "\nAnd Green Team got Skunked";&nbsp; &nbsp; }&nbsp; &nbsp; return skunkText;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java