为了账号安全,请及时绑定邮箱和手机立即绑定

合并多个 if 语句

合并多个 if 语句

呼唤远方 2023-09-13 10:28:11
我有以下代码,它依赖于多个不同的 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 语句中使用(太糟糕了),但是还有其他类似的东西可以帮助集中我的代码吗?
查看完整描述

3 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

我仍然认为自己相当新,但这就是我会写的内容。我编辑了几次。


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 (不是常量)。否则将其分配给其他东西并使用它


不假设“hasSkunk”意味着至少有一支球队的得分<90,否则最后一个 else if 会变成 else ...


由于性能原因,“Else if”在理论上会更好 - 只有在其中一个为真时才会评估 if。这在这里不是问题,但如果其中一个条件是某种重型方法的结果......


像这样的一堆 Else If 的工作方式非常类似于带有中断的 switch。


查看完整回答
反对 回复 2023-09-13
?
撒科打诨

TA贡献1934条经验 获得超2个赞

您可以尝试三元运算符 ?: 。上面代码中的示例我只是简化了某些部分。你可以尝试其他类似的东西


        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");

                }

            }

        }


查看完整回答
反对 回复 2023-09-13
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

更干净一点,因为只在一个地方设置 textField,不需要 == 一个布尔值,if 应该分支,因为如果一个成功,另一个就会失败。与初始响应略有不同。


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; 

}


查看完整回答
反对 回复 2023-09-13
  • 3 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信