如何将 if else 转换为 && ||如果结果相同

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

如何将 if else 替换为 && ||如果在这些情况下每个条件的输出都相同

在达到输出之前需要满足多个不同的条件,并且每种情况都有相同的输出。

请注意,条件和输出只是占位符,因为这主要是伪代码问题

案例1:

if(condition 1){
    if(condition 2){
        if(condition 3){
            if(condition 4){
                if(condition 5){
                    return(1);
                }
                if(condition 6){
                    if(condition 7){
                        return(1);
                    } 
                }
            }else{
                if(condition 10){
                     return(1);
                }
            }
        }
    }
}

案例2:

if(condition 1){
    if(condition 2){
        if(condition 3){
            if(condition 4){
                if(condition 6){
                    if(condition 8){
                         return(2);
                        }   
                }else{
                    if(condition 9){
                        return(2);
                    }
                }
            }
        }else{
            if(condition 11){
                  return(2);
            }
        }
    }else{
        if(condition 12){
            return(2);
        }
    }
}else if(condition 13){
    if(condition 14){
        return(2);
    }
}
java algorithm if-statement logic pseudocode
1个回答
0
投票

你在想这样的事情吗?

if(condition 1 && condition 2 && condition 3 && condition 4){
if(condition 5){ return 1}
if(condition 6 && condition 7){ return 1}
...
}

不过,这肯定会有助于澄清一些!

© www.soinside.com 2019 - 2024. All rights reserved.