【问题标题】:How to create a console Timer for quiz Application如何为测验应用程序创建控制台计时器
【发布时间】:2020-09-17 22:52:51
【问题描述】:

我想创建一个 30 秒的计时器,30 秒后计时器应该结束并显示一些消息。

在这 30 秒内,如果用户未能输入选项,我希望用户输入一个选项,然后我需要显示一些消息,例如超时。

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);
}

但这不是我想实施的,请分享一分钟来帮助我。

【问题讨论】:

  • 先生您能帮我解决这个问题吗

标签: c# timer


【解决方案1】:

我认为你想要使用的是一个计时器。

首先添加如下命名空间。

using System.Timers;

接下来添加一个模块化/全局变量来进行计时。

static Timer questionTimer = new Timer(30000) //Time in milliseconds to fire event..

接下来,您需要连接到 Elapsed 事件并启用计时器。

questionTimer.Elapsed += QuestionTimer_Elapsed;
questionTimer.Enabled = true;

你的超时方法可能看起来像这样。

private static void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    Console.WriteLine("Time up!");
    questionTimer.Stop();
}

最后,您将在每次显示新问题时启动此计时器。 (如果问题回答正确,请不要忘记停止计时器,否则事件仍会触发)

questionTimer.Start(); //Start question timer...
Console.Write("What is 5+5? "); //Ask the question...
string ans = Console.ReadLine(); //Get the user input...
if (ans == "10")
{
    //If it's correct stop timing...
    Console.WriteLine("Correct!");
    questionTimer.Stop();
}

我希望这对您有所帮助,如果您需要任何进一步的解释,请发表评论。 :)

【讨论】:

  • 先生,我需要这样的东西:问题:什么是 xyz?
  • 它的选项和控制台中的计时器显示您还剩“ z”秒
  • 如果用户在 t 秒内输入他的选择,那么很好,否则我必须显示超时。如果你能帮我这样做会很棒
  • 这就是它的作用,我在您回答问题的地方添加了更多内容,它有帮助吗?
  • 先生,如果您能告诉我如何在控制台中显示,就像您还剩 z 秒一样,它真的对我很有帮助
【解决方案2】:

好的,我想我在之前的回答中误解了你的问题。

你想要的是一条倒计时剩余时间的线?

与连接计时器的过程类似,但还创建了一个名为 secondCounter 的 int 变量,我们将计时器的间隔设置为 1000。(1 秒)。

我们的 timer elapsed 方法现在看起来像这样:

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.Start();
Console.WriteLine("What is 5 + 5?");

var input = Console.ReadLine();

Console.WriteLine($"You anwsered {input}");
questionTimer.Stop();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2012-06-03
    相关资源
    最近更新 更多