示例#1
0
        static void UseStopwatch(MyWatch myWatch, string command)
        {
            switch (command)
            {
            case "start":
                try
                {
                    myWatch.Start();
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("stopWatch is already started\n");
                }
                break;

            case "stop":
                try
                {
                    myWatch.Stop();
                    Console.WriteLine("Duration: {0}\n", myWatch.GetDuration());
                }
                catch (InvalidOperationException)
                {
                    Console.WriteLine("stopWatch is not started\n");
                }
                break;

            default:
                break;
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            MyWatch myWatch = new MyWatch();

            while (true)
            {
                Console.WriteLine("Enter 'start' to start Stopwatch\nEnter 'stop' to end Stopwach\nEnter any key to exit:\n");
                var input = Console.ReadLine().ToLower();

                if (input == "start" || input == "stop")
                {
                    UseStopwatch(myWatch, input);
                }
                else
                {
                    return;
                }
            }
        }