在控制台应用程序中使用秒表

Using stopwatch in a console application

我想在 C# 控制台中制作一个简单的秒表。当您按 S 键时,您启动秒表和 Q 键停止秒表。最后经过的时间将显示在 Hours:Min:Sec 中。直到现在秒表开始,但当它停止时,它没有得到经过的时间。

static void Main(string[] args)
{
    Console.WriteLine("The stopwatch, press S to begin and Q to stop");
    var UserInput = Console.ReadLine();
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    stopWatch.Start();

    switch (UserInput)
    {
        case "s":
            stopWatch.Start();;
            break;
        case "q":
            stopWatch.Stop();

            break;
        default:
            Console.WriteLine("You did something wrong");
            break;
    }

    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value. 
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 2);
    Console.WriteLine("RunTime " + elapsedTime);

    Console.ReadLine();
}

这是输出:

https://i.stack.imgur.com/VKtCQ.png

这是作业的味道,但是:

你的主要问题不是你的代码不会 "work",而是你直接飞过代码的整个秒表部分。您没有循环来重复获取用户输入,因此从本质上讲,您的用户首先输入什么并不重要。试一试,在你的 switch 语句之后休眠一秒钟 System.Threading.Thread.Sleep(1000),然后输入各种用户输入。

我建议您查看 do while 循环以使您的用户输入被反复接受。原则上,如果您能弄清楚如何循环输入,您的代码的每一部分都可能有效。

P.S。我不明白你为什么要将毫秒数除以 2。这会让你以后很头疼。

你错过了一个循环。您可能按 's' 开始,当您按 'q' 时,您实际上是在按下最后一个 Console.ReadLine()

如果你添加一个循环,一切正常:

    static void Main(string[] args)
    {
        Console.WriteLine("The stopwatch, press S to begin and Q to stop");
        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
        stopWatch.Start();

        var done = false;

        while (!done)
        {
            var UserInput = Console.ReadLine();

            switch (UserInput)
            {
                case "s":
                    stopWatch.Start();
                    break;
                case "q":
                    stopWatch.Stop();
                    done = true;
                    break;
                default:
                    Console.WriteLine("You did something wrong");
                    break;
            }
        }

        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value. 
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 2);
        Console.WriteLine("RunTime " + elapsedTime);

        Console.ReadLine();
    }

希望对您有所帮助:)

你可以这样做:

    static void Main(string[] args)
    {
        Console.WriteLine("The stopwatch, press S to begin and Q to stop");
        var UserInput = Console.ReadLine();
        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
        stopWatch.Start();


    Decide:
        switch (UserInput.ToLower())
        {
            case "s":
                stopWatch.Start();

                while (!Console.KeyAvailable)
                {
                    // Get the elapsed time as a TimeSpan value.
                    TimeSpan ts = stopWatch.Elapsed;
                    Console.Clear();
                    // Format and display the TimeSpan value. 
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                        ts.Hours, ts.Minutes, ts.Seconds,
                        ts.Milliseconds / 2);
                    Console.WriteLine("RunTime " + elapsedTime);
                    Thread.Sleep(1000);
                }

                UserInput = Console.ReadLine();
                if (UserInput.ToLower() != "q")
                {
                    goto Decide;
                }
                break;
            case "q":
                stopWatch.Stop();

                break;
            default:
                Console.WriteLine("You did something wrong");
                break;
        }
    }