猿问

使用 if 条件检查参数的最有效和最优雅的方法

我有 3 个参数可以检查某些条件。


例如我的代码如下,


    public static string checkIfConnditions(string msg, int score, int age)

    {

        var response = "";


        if (msg == "hello" && score >= 20 && age <= 25)

        {

            response = "All para success";

        }

        if (msg != "hello" && score >= 20 && age <= 25)

        {

            response = "Unmatching message";

        }

        if (msg == "hello" && score < 20 && age <= 25)

        {

            response = "Score not satisfied";

        }

        if (msg == "hello" && score >= 20 && age > 25)

        {

            response = "Age not satisfied";

        }

        if (msg != "hello" && score < 20 && age <= 25)

        {

            response = "Unmatiching message & Score not satisfied ";

        }

        if (msg != "hello" && score >= 20 && age > 25)

        {

            response = "Unmatiching message & Age not satisfied";

        }

        if (msg == "hello" && score < 20 && age > 25)

        {

            response = "Age & Score not satisfied";

        }

        if (msg != "hello" && score < 20 && age > 25)

        {

            response = "All parameter unsatisfied";

        }

        return response;

    }}

有 3 个参数,根据其值可以发生 8 个概率。在这里,我检查了上面的代码。但它看起来很难看,我认为这不是最好的方法。什么是最有效和最优雅的方式来做到这一点



富国沪深
浏览 123回答 3
3回答

慕斯王

先把它分组怎么样:public string checkIfConnditions(string msg, int score, int age){&nbsp; &nbsp;var response = "";&nbsp; &nbsp;if (msg == "hello") {&nbsp; &nbsp; &nbsp; &nbsp;response = score > 20 ?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; age > 25 ? "Age not satisfied" : "All para success"&nbsp; &nbsp; &nbsp; &nbsp; : age < 25 ? "Score not satisfied" : "Age & Score not satisfied";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; (score > 20)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = age < 25 ? "Unmatching message" : "Unmatiching message & Age not satisfied" ;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = age < 25 ? "Unmatiching message & Score not satisfied " : "All parameter unsatisfied" ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return response;&nbsp;}如果相等,您还需要注意条件。例如if (msg == "hello" && score > 20 && age < 25){&nbsp; &nbsp; response = "All para success";}//and ...if (msg == "hello" && score < 20 && age < 25){&nbsp; &nbsp; response = "Score not satisfied";}// what if score == 20 ?withif else声明,或者The conditional operator (?:)我们可以避免这种情况更新if(msg == "hello"){&nbsp; &nbsp; if(score < 20)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; response = age > 25 ? "Age & Score not satisfied" : "Score not satisfied";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; response = age > 25 ? "Age not satisfied" : "All para success";&nbsp; &nbsp; }} else {&nbsp; &nbsp; if(score < 20)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; response = age > 25 ? "All parameter unsatisfied" : "Unmatiching message & Score not satisfied ";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; response = age > 25 ? "Unmatiching message & Age not satisfied" : "Unmatching message";&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}

MM们

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> Errors = new List<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int chk = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( msg != "hello" )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Errors.Add( "Unmatching message" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( score < 20 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Errors.Add( "Score not satisfied" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( age > 25 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Errors.Add( "Age not satisfied" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( Errors.Count == 0 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "All para success";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if ( Errors.Count == 3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "All parameter unsatisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return String.Join( " & ", Errors );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }** 代码已编辑,因为我将 String.Join 输入错误为 String.Format **或者,如果您想逐个回答,也可以使用字节&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int flag = 0x0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( msg == "hello" )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag |= 0x1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( score > 20 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag |= 0x2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( age < 25 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag |= 0x4;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch ( flag )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x7:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "All para success";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Unmatching message";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Score not satisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Unmatiching message & Age not satisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Score not satisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Unmatiching message & Score not satisfied ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 0x1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "Score not satisfied & Age not satisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = "All parameter unsatisfied";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

阿晨1998

一种可能的方法是创建字典,其中包含您的真值表和相应的响应,例如:private readonly Dictionary<(bool isMsgValid, bool isScoreValid, bool isAgeValid), string> _responses&nbsp;&nbsp; &nbsp; = new Dictionary<(bool, bool, bool), string>(){&nbsp; &nbsp; [(true, true, true)] = "All para success",&nbsp; &nbsp; [(false, false, false)] = "All parameter unsatisfied",&nbsp; &nbsp; [(false, true, true)] = "Unmatching message",&nbsp; &nbsp; [(true, false, true)] = "Score not satisfied",&nbsp; &nbsp; [(true, true, false)] = "Age not satisfied",&nbsp; &nbsp; [(false, false, true)] = "Unmatiching message & Score not satisfied",&nbsp; &nbsp; [(false, true, false)] = "Unmatiching message & Age not satisfied",&nbsp; &nbsp; [(true, false, false)] = "Age & Score not satisfied"};public string checkIfConnditions(string msg, int score, int age)&nbsp; &nbsp; => _responses[(msg == "hello", score > 20, age < 25)];由您决定哪个变体更优雅,这只是可能的解决方案之一。请注意,这里使用C# 7.0 features, ValueTuple 字典键和表达式主体方法checkIfConnditions。编辑这是我用于测试的示例:public static class Program{&nbsp; &nbsp; private static readonly Dictionary<(bool isMsgValid, bool isScoreValid, bool isAgeValid), string> _responses&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; = new Dictionary<(bool, bool, bool), string>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [(true, true, true)] = "All para success",&nbsp; &nbsp; &nbsp; &nbsp; [(false, false, false)] = "All parameter unsatisfied",&nbsp; &nbsp; &nbsp; &nbsp; [(false, true, true)] = "Unmatching message",&nbsp; &nbsp; &nbsp; &nbsp; [(true, false, true)] = "Score not satisfied",&nbsp; &nbsp; &nbsp; &nbsp; [(true, true, false)] = "Age not satisfied",&nbsp; &nbsp; &nbsp; &nbsp; [(false, false, true)] = "Unmatiching message & Score not satisfied",&nbsp; &nbsp; &nbsp; &nbsp; [(false, true, false)] = "Unmatiching message & Age not satisfied",&nbsp; &nbsp; &nbsp; &nbsp; [(true, false, false)] = "Age & Score not satisfied"&nbsp; &nbsp; };&nbsp; &nbsp; public static string checkIfConnditions(string msg, int score, int age)&nbsp; &nbsp; &nbsp; &nbsp; => _responses[(msg == "hello", score > 20, age < 25)];&nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("hello", 45, 20));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("hello", 45, 30));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("hello", 10, 20));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("hello", 10, 30));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("goodbye", 45, 20));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("goodbye", 10, 30));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("goodbye", 45, 20));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(checkIfConnditions("goodbye", 10, 30));&nbsp; &nbsp; }}请注意,在这种情况下,_responsesandcheckIfConnditions必须是静态的。
随时随地看视频慕课网APP
我要回答