电话状态机
enum State { 脱机, 连接中, 已连接, 通话保持 } enum Trigger { 拨打电话, 挂断电话, 接收来电, 通话保持, 取消等待, 语音留言 } class Program { public static Dictionary<State, List<(Trigger, State)>> rules = new Dictionary<State, List<(Trigger, State)>> { [State.脱机] = new List<(Trigger, State)> { (Trigger.拨打电话,State.连接中) }, [State.已连接] = new List<(Trigger, State)> { (Trigger.挂断电话,State.脱机), (Trigger.接收来电,State.已连接) }, [State.连接中] = new List<(Trigger, State)> { (Trigger.语音留言,State.脱机), (Trigger.挂断电话,State.脱机), (Trigger.通话保持,State.通话保持), }, [State.通话保持] = new List<(Trigger, State)> { (Trigger.取消等待,State.连接中), (Trigger.挂断电话,State.脱机) }, }; static void Main(string[] args) { var state = State.脱机; while (true) { Console.WriteLine($"手机当前状态是{state}"); Console.WriteLine("选择一个触发:"); for (int i = 0; i < rules[state].Count; i++) { var (t, _) = rules[state][i]; Console.WriteLine($"{i}.{t}"); } int input = int.Parse(Console.ReadLine()); var (_,s)= rules[state][input]; state = s; } } }