我需要一个可以为我的变量设置新值的 C# 计时器

I need a C# timer that can set a new value to my variable

我是昨天入职的新 C# 程序员。

我尝试编写自己的简单猜谜游戏。用户获得一个主题,并必须在 2 分钟内猜出一个秘密单词。

我已经编写了时间为 0 时发生的操作的代码,但是我没有编写这样的计时器。基本上我需要一个计时器来设置我的 bool outofTime 触发结束到 true.

这是我的代码,所以你可以看看我试图描述的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace EigenesProjekt
{
    class Program
    {
        private static void Main(string[] args)
        {
            string userName = ("");
            string userAge = ("");
            bool confirm = true;
            string trueFalse = ("");

            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.DarkBlue;

            Console.WriteLine("Enter your username: ");
            userName = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Enter your age: ");
            userAge = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Hello " + userName + ", you're " + userAge + " years old. Is this TRUE or FALSE?");
            trueFalse = Console.ReadLine();
            MainGame(userName, userAge);
            switch (trueFalse)
            {
                case "TRUE":
                    confirm = true;
                    break;
                case "FALSE":
                    confirm = false;
                    break;
                default:
                    Console.WriteLine("Invalid confirmation. Restart the application and use TRUE or FALSE");
                    Console.ReadLine();
                    break;

            }

            if (confirm)
            {
                Console.WriteLine("Okay, thanks for confirming your personal information");
                Console.ReadLine();


            }
            else
            {
                Console.WriteLine("In this case please restart the application and try again");
                Console.ReadLine();
            }

        }

        public static void MainGame(string userName, string userAge)
        {
            string category = "";
            string guess = "";
            string rightAnswer = "";
            bool outofTime = false;


            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine("                                        username: " + userName + "          age: " + userAge);
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("Your guessing category is " + category + ". You have 2 Minutes to get the right answer.");
            Console.WriteLine("Main thread: starting a timer");

            while (guess != rightAnswer)
            {
                if (outofTime == false && guess != rightAnswer)
                {
                    Console.WriteLine("Wrong answer. Try again");
                    guess = Console.ReadLine();
                }
                else if (outofTime == true)
                {
                    Console.WriteLine("You are out of time and lost the game. Try again by restarting the application");
                    Console.ReadLine();
                }
            }
            while (guess == rightAnswer)
                {
                if (guess == rightAnswer && outofTime == false)
                {
                    Console.WriteLine("You won the Game! The secret word was: " + rightAnswer);
                    Console.ReadLine();

                }
            }
        }
    }
}

对于一日程序员来说,这是一个非常好的开始。其实你想做的事情有点棘手。 Console.ReadLine() 冻结当前等待用户输入的线程,因此您无法深入研究代码。一个人创建了一个实用程序 class 来为您完成这项工作 >>>there<<<,您可以像这样在您的代码中使用它:

Reader.ReadLine(1000); // for waiting 1 second max

而不是:

Console.ReadLine();

如果用户需要更多时间响应,他的代码将抛出 TimeoutException。 您可以这样处理“超时”:

try {
    var line = Reader.ReadLine(1000);
    Console.WriteLine("user typed : " + line);
}
catch(TimeoutException) {
    Console.WriteLine("user take too much time to answer");
}

创建基本计时器的一种方法是捕获当前时间,向其添加一些分钟数,并将其保存为 endTime。然后你可以检查是否DateTime.Now > endTime,如果是,那么我们已经过了结束时间。

例如:

Console.Write("Enter your first guess to start the clock: ");
string guess = Console.ReadLine();

// Set end time to 2 minutes from now
DateTime endTime = DateTime.Now.AddMinutes(2);

// Continue to get user input while their answer is incorrect and there's still time left
while (guess != rightAnswer && DateTime.Now < endTime)
{
    TimeSpan timeRemaining = endTime - DateTime.Now;

    Console.WriteLine("Incorrect guess. You have " + 
        timeRemaining.Minutes + " minutes and " +
        timeRemaining.Seconds + " seconds left...");

    Console.Write("Enter your guess: ");
    guess = Console.ReadLine();
}

if (guess == rightAnswer && DateTime.Now <= endTime)
{
    Console.WriteLine("You won the Game! The secret word was: " + rightAnswer);
}
else
{
    Console.WriteLine("You are out of time and lost the game.");
}