0

我需要显示在控制台中运行的计时器。我使用秒表:

System.Diagnostics.Stopwatch 秒表 = new System.Diagnostics.Stopwatch();

public void startTime()
{
    stopwatch.Start();
}

public void GetTime(){
    stopwatch.Stop();
    TimeSpan timeSpan = stopwatch.Elapsed;
    var timeElapsed = (string.Format("\nTime Elapsed: {0} minute(s) {1} second(s)",
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n")).ToString();
}

我需要在控制台中显示计时器正常运行:00:00:01、00:00:02、00:00:03 ... 等

我怎样才能做到这一点?

4

2 回答 2

2

Console.Clear

Console.SetCursorPosition

方法

于 2011-05-17T19:32:30.890 回答
0

像这样的东西?

Stopwatch sw = new Stopwatch();
sw.Start();

while (true)
{
    Console.SetCursorPosition(0, 0);
    Console.Write(sw.Elapsed.ToString("g"));

    if (sw.Elapsed.TotalMinutes > 30)
        break;
}
于 2011-05-17T19:39:58.483 回答