【发布时间】:2026-01-05 18:30:01
【问题描述】:
我正在制作一个每分钟单词的程序(用于课堂),根据我一直在研究的内容,这应该有效。基本上,如果计时器达到 10 秒,我希望秒表停止(尽管这不是必需的)并阻止用户再输入。我将如何实现这一目标?
public void Timer30()
{
double userCharcount = 0;
string userType;
int timeInSeconds = 10;
//new instance of stopwatch
Stopwatch stopWatch = new Stopwatch( );
//call level 1 words from wordbank
Console.WriteLine(WordBank.LevelOneWords);
stopWatch.Start( );
//**this doesn't seem to work**
if ( stopWatch.Elapsed.Seconds >= timeInSeconds )
{ Console.WriteLine("Time's up! Nice work.");
stopWatch.Stop( );
Console.ReadKey();
}
userType = Console.ReadLine( );
stopWatch.Stop( );
//capture number of characters user types to calculate WPM
userCharcount = userType.Length;
// 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 / 10);
Console.WriteLine("\nNice job! You finished in " + elapsedTime + "!");
Thread.Sleep(2000);
//CalculateWPMEasy(userCharCount);
}
【问题讨论】: