【发布时间】: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