C# 我如何显示一条消息说您的时间到了,然后终止程序

C# 我如何显示一条消息说您的时间到了,然后终止程序,c#,timer,C#,Timer,我已经创建了一个30秒的计时器,我将使用每个方法。我如何显示足够长的消息,让他们阅读,然后终止程序 namespace Calculator{ class Program { static void Main(string[] args) { System.Timers.Timer timer = new System.Timers.Timer(5000); Console.WriteLine("

我已经创建了一个30秒的计时器,我将使用每个方法。我如何显示足够长的消息,让他们阅读,然后终止程序

namespace Calculator{

    class Program
    {
        static void Main(string[] args)
        {
            System.Timers.Timer timer = new System.Timers.Timer(5000);

            Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
            "if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
        //These are the instructions
        timer.Start();
        q1();
        timer.Stop();
        }

        static string q1() //Return type is a string as a string prompting the user will ask them to try again
        {
            Console.WriteLine("1+1"); //This is the question
            int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable
            if (answer == 2) //If the users input is equal to 2 
            {
                Console.WriteLine("Correct");//Tells the user that they are correct
            }
            return "Incorrect, try again";//Promts the user to trya again

        }
    }
}
您可以这样做:

Console.WriteLine("Press any key to continue");
Console.ReadLine();
控制台将等待,直到按下任何键继续或退出程序(如果这是最后一件事)

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
        "if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
        //These are the instructions

        q1();
        Console.WriteLine("Press any key to continue");
        Console.ReadLine();
    }

    static Stopwatch timer = Stopwatch.StartNew();
    static void q1() //Return type is a string as a string prompting the user will ask them to try again
    {
        Console.WriteLine("1+1"); //This is the question
        int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable

        if (timer.ElapsedMilliseconds > 30000)
        {
            Console.WriteLine("It took you too much time");
        }
        if (answer == 2) //If the users input is equal to 2 
        {
            Console.WriteLine("Correct");//Tells the user that they are correct
        }
        else
        {
            Console.WriteLine("Try again");
            q1();
        }
    }
}

您的方法中也不需要任何返回,您还没有在任何地方使用过它。

嗯,在程序执行完成后显示控制台的常用方法,是等待用户输入,例如使用Console.ReadLine作为主方法的最后一行代码。您需要使用计时器事件。@xdtTransform它读取q1方法int answer=Convert.ToInt32Console.ReadLine中的用户输入,但这只会在用户实际将某个内容输入到控制台,我的意思是它应该显示,一旦计时器结束,工作正常,谢谢,还有一件事,如果方法是正确的,我如何停止它,因为我将创建更多类似于q1的方法,我希望在调用下一个方法时计时器再次启动。你必须将线程实例放在类字段中,为了使它在像q1这样的方法中可见,请使用方法thread.Abort,以防给出正确答案。只是根据你的问题修改了代码。
class Program
{

    static ThreadStart ThreadStart = new ThreadStart(Counter);
    static Thread Thread = new Thread(ThreadStart)
    {
        Priority = ThreadPriority.Highest
    };
    static void Main(string[] args)
    {

        Console.WriteLine("INSTRUCTIONS - You have 30 seconds to answer each question correctly, once you get the question right the next question will appear," +
        "if you get a question wrong the console will display INCORRECT and you will have until the end of the 30 seconds to answer it correctly.");
        //These are the instructions



        Thread.Start();

        q1();
        Console.WriteLine("Press any key to continue");
        Console.ReadLine();
    }

    static Stopwatch timer = Stopwatch.StartNew();
    static void Counter()
    {
        if(timer.ElapsedMilliseconds < 30000)
        {
            Thread.Sleep(1000);
            Counter();
        }
        else
        {
            Console.WriteLine("Too late");

            Environment.Exit(0);
        }

    }

    static void q1() //Return type is a string as a string prompting the user will ask them to try again
    {
        Console.WriteLine("1+1"); //This is the question
        int answer = Convert.ToInt32(Console.ReadLine());// Can't apply int to a readline, so convert the useres input to an int so you can apply an int variable

        if (answer == 2) //If the users input is equal to 2 
        {
            Console.WriteLine("Correct");//Tells the user that they are correct
            Thread.Abort();
        }
        else
        {
            Console.WriteLine("Try again");
            q1();
        }
    }
}