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