【问题标题】:I need a C# timer that can set a new value to my variable [closed]我需要一个可以为我的变量设置新值的 C# 计时器 [关闭]
【发布时间】:2020-08-05 19:57:58
【问题描述】:

我是一名昨天开始的新 C# 程序员。

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

我已经对时间为 0 时发生的动作进行了编码,但是我在编写这样的计时器时失败了。基本上我需要一个计时器来设置我的布尔值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();

                }
            }
        }
    }
}

【问题讨论】:

  • 您好,欢迎来到 SO! I already coded the action that happen when the time is 0, but I failed at coding a timer like this,你试过的计时器代码在哪里,什么不起作用?你有looked and readSystem.Timers.Timer 类的实际文档,看看它是如何工作的吗?如果没有,那将是一个很好的开始。如果您已尝试过但无法正常工作,请返回并edit您的问题,以便我们进一步帮助您。
  • 我尝试了一些代码,但完全没有成功,所以我删除了它。我要看看这个类文档。谢谢你:)
  • 不客气,祝你好运!如果您仍有具体问题,请回来更新您的帖子,我们可以进一步帮助您。
  • 为什么不直接获取当前时间,加上您希望他们完成游戏的时间,然后在循环中根据该值检查当前时间?这不是一个超级准确的计时器,但它会是一个好的开始。

标签: c# timer console console-application


【解决方案1】:

创建基本计时器的一种方法是捕获当前时间,添加一些分钟数,然后将其保存为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.");
}

【讨论】:

    【解决方案2】:

    对于一日程序员来说,这是一个非常好的开始。实际上,您尝试做的事情有点棘手。 Console.ReadLine() 冻结当前线程等待用户输入,所以你不能深入你的代码。一个人创建了一个实用程序类来为你完成这项工作>>>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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2021-05-15
      • 1970-01-01
      • 2021-09-16
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多