其他分享
首页 > 其他分享> > 状态模式 03

状态模式 03

作者:互联网

简单解锁例子

    enum State
    {
        锁定,
        失败,
        已解锁
    }
    class Program
    {
        static void Main(string[] args)
        {

            string code = "132";
            var state = State.锁定;
            var strb = new StringBuilder();
            Console.WriteLine("请输入密码:");
            while (true)
            {
                switch (state)
                {
                    case State.锁定:
                        strb.Append(Console.ReadKey().KeyChar);
                        if (strb.ToString()==code)
                        {
                            state = State.已解锁;
                            break;
                        }
                        if (!code.StartsWith(strb.ToString()))
                        {
                            state = State.失败;
                        }
                        break;
                    case State.失败:
                        Console.CursorLeft = 0;
                        Console.WriteLine("密码错误,解锁失败");
                        strb.Clear();
                        state = State.锁定;
                        break;
                    case State.已解锁:
                        Console.CursorLeft = 0;
                        Console.WriteLine("已解锁");
                        return;
                        break;
                    default:
                        break;
                }
            }

        }
    }

 

标签:03,Console,状态,解锁,state,模式,break,State,strb
来源: https://www.cnblogs.com/Zingu/p/16446595.html