合并多个 if 语句

Posted

技术标签:

【中文标题】合并多个 if 语句【英文标题】:Consolidating multiple if statements 【发布时间】:2020-01-28 21:13:54 【问题描述】:

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

【问题讨论】:

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

不假设'hasSkunk'意味着至少有一个团队有

'Else if' 在这里理论上更好,因为它的性能更好 - 只有在一个为真之前才会评估 if。这不是问题,但如果其中一个条件是大量方法的结果......

这样的一堆 Else If 非常类似于带中断的 switch。

【讨论】:

【参考方案2】:

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

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

【讨论】:

感谢三元运算符的提示,好东西让我去看看。【参考方案3】:

更简洁一点,因为只需将 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; 

【讨论】:

以上是关于合并多个 if 语句的主要内容,如果未能解决你的问题,请参考以下文章

将两个 if 语句合并为一个的 Perl 单行器

如何在单个合并语句中执行多个更新

一次合并到多个语句 Oracle SQL

合并多个sql更新语句(Oracle)

c#合并与设置为null时的if语句相同吗?

合并 SQL 多个插入语句不是一个选项