C# 如何为测验应用程序创建控制台计时器
我想创建一个30秒的计时器,30秒后计时器应该结束并显示一些消息 在这30秒内,我希望用户输入一个选项,如果他未能输入一个选项,那么我需要显示一些消息,如超时C# 如何为测验应用程序创建控制台计时器,c#,timer,C#,Timer,我想创建一个30秒的计时器,30秒后计时器应该结束并显示一些消息 在这30秒内,我希望用户输入一个选项,如果他未能输入一个选项,那么我需要显示一些消息,如超时 public void AskEasyQues(EasyQuestion easyQuestion) { System.Console.WriteLine("Here is Your:"+sQcount+" Question:"); System.Console.WriteLine(
public void AskEasyQues(EasyQuestion easyQuestion)
{
System.Console.WriteLine("Here is Your:"+sQcount+" Question:");
System.Console.WriteLine("***********************************");
System.Console.WriteLine("Question is of The Category:"+easyQuestion.MCat);
System.Console.WriteLine("***********************************");
System.Console.WriteLine(easyQuestion.MDescription);
System.Console.WriteLine("--------------------------------------");
System.Console.WriteLine("1:"+easyQuestion.MOption1+" "+"2:"+easyQuestion.MOption2);
System.Console.WriteLine();
System.Console.WriteLine("3:"+easyQuestion.MOption3+" "+"4:"+easyQuestion.MOption4);
System.Console.WriteLine();
System.Console.WriteLine("Enter your Choice:");
for (int a = 60; a >= 0; a--)
{
Console.Write("\rGenerating Preview in {0:00}", a);
System.Threading.Thread.Sleep(1000);
}
string ans = Console.ReadLine();
if(ans == easyQuestion.MAnswer)
{
mNoOfQuesAnswerd++;
System.Console.WriteLine();
System.Console.WriteLine("------Well Played Champion!!!!!!-----");
sPlayerScore=sPlayerScore+100;
}
else
{
System.Console.WriteLine();
System.Console.WriteLine("------Wrong Choice Lets Move On--------");
}
System.Console.WriteLine();
System.Console.WriteLine("Press any Key To Continue For Next Question");
Console.ReadLine();
System.Console.WriteLine();
System.Console.WriteLine("----------------------------");
sQcount = sQcount + 1;
Console.Clear();
}
这是我的代码,我还尝试了使用for循环的简单倒计时,如下所示:
for (int a = 60; a >= 0; a--)
{
Console.Write("\rGenerating Preview in {0:00}", a);
System.Threading.Thread.Sleep(1000);
}
但这不是我想要实现的,请分享一分钟来帮助我。我想你想要使用的是计时器 首先添加以下名称空间
using System.Timers;
接下来添加一个模块化/全局变量进行计时
static Timer questionTimer = new Timer(30000) //Time in milliseconds to fire event..
接下来,您需要连接到经过的事件并启用计时器
questionTimer.Elapsed += QuestionTimer_Elapsed;
questionTimer.Enabled = true;
questionTimer.Start();
Console.WriteLine("What is 5 + 5?");
var input = Console.ReadLine();
Console.WriteLine($"You anwsered {input}");
questionTimer.Stop();
你的计时方法可能是这样的
private static void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Time up!");
questionTimer.Stop();
}
最后,每次显示新问题时,都会启动计时器。(如果问题回答正确,不要忘记停止计时器,否则事件仍将触发)
我希望这对您有所帮助,如果您需要任何进一步的解释,请留下评论。:) 好吧,我想我误解了我先前回答中的问题 你想要的是一条倒计时的线 连接计时器的类似过程,但也创建一个名为secondCounter的int变量,我们将计时器的间隔设置为1000。(1秒) 我们的计时器运行时间方法现在看起来如下所示:
private static void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
secondCounter = secondCounter + 1;
Console.SetCursorPosition(0, 2); //This is a row number...
Console.Write("\rSeconds Remaining: {0}: ", 30 - secondCounter);
if (secondCounter >= 30)
{
Console.WriteLine("Time's up!");
questionTimer.Stop();
}
}
然后,您将在计时器之外处理您的问题逻辑
questionTimer.Elapsed += QuestionTimer_Elapsed;
questionTimer.Enabled = true;
questionTimer.Start();
Console.WriteLine("What is 5 + 5?");
var input = Console.ReadLine();
Console.WriteLine($"You anwsered {input}");
questionTimer.Stop();
先生,你能帮我解决这个问题吗先生,我需要这样的东西:问题:xyz是什么?它的选项和控制台中的计时器显示你还有“z”秒如果用户在t秒中输入他的选择,那么很好,否则我必须显示timedout。如果你帮我做这件事,那就太好了,这就是它的功能,我在回答问题的地方补充了一点,这有帮助吗?先生,如果你能告诉我如何在控制台中显示,就像你还有z秒一样,这真的帮助了我