如何计算秒数然后在条件中使用它们?

时间:2014-03-24 11:44:23

标签: c# seconds

我是一名新手程序员,我现在已经尝试处理这个问题超过2个小时了。我希望程序做的是计算秒数,如果它超过第二秒(什么?),做一些事情,例如显示" Game Over"或类似的东西。问题是,在这2秒钟过后,程序没有做任何事情。问题可能是什么?

编辑:好的,这是这背后的全部信息。用户必须在2秒内按下与屏幕上显示的字符相对应的键。如果用户没有在2秒钟内按下键或按错键,则游戏必须结束,但它没有按预期工作lol

到目前为止,这是完整的代码(是的,我知道goto糟透了,我稍后会用循环更改它):

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

class Game
{
    static void Main()
    {
    Start:
        Console.CursorVisible = false;
        Random rnd = new Random();
        int row = rnd.Next(1, 80);
        int col = rnd.Next(1, 25);
        int chars = rnd.Next(0, 62);
        string lettersAndChars = "";
        lettersAndChars = lettersAndChars.ToUpper();
        Console.SetCursorPosition(row, col);
        if (chars <= 10)
        {
            lettersAndChars += (char)(chars + '0');
        }
        else
        {
            lettersAndChars += (char)(chars + 'A');
        }
        lettersAndChars = lettersAndChars.ToUpper();
        Console.WriteLine(lettersAndChars);
        DateTime endTime = DateTime.Now.AddSeconds(2);
        var keyPress = Console.ReadKey();
        string keyPressString = keyPress.KeyChar.ToString();
        keyPressString = keyPressString.ToUpper();
        if (keyPressString == lettersAndChars && DateTime.Now < endTime)
        {
            Console.Clear();
            goto Start;
        }
        else if (keyPressString != lettersAndChars || DateTime.Now > endTime)
        {
            Console.Clear();
            Console.WriteLine("Game Over");
        }


    }
}

2 个答案:

答案 0 :(得分:0)

我不会使用Timer。您可以执行以下操作:

DateTime endTime = DateTime.Now.AddSeconds(2);
while (!Console.KeyAvailable && DateTime.Now < endTime)
    Thread.Sleep(1);
if (Console.KeyAvailable)
{
    var keyPress = Console.ReadKey();
    string keyPressString = keyPress.KeyChar.ToString();
    keyPressString = keyPressString.ToUpper();
    if (keyPressString == lettersAndChars)
    {
        Console.Clear();
        goto Start;
    }
}
Console.Clear();
Console.WriteLine("Game Over");

答案 1 :(得分:0)

@Ulugbek Umirov的回答有效!谢谢!以下是它现在的样子(我之前也改变了很多代码):

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

    class Game
    {
    static void Main()
    {
        bool isTrue = true;
        while (isTrue == true)
        {
        Console.BufferHeight = 25;
        Console.BufferWidth = 80;
        Console.CursorVisible = false;
        Random rnd = new Random();
        int row = rnd.Next(1, 80);
        int col = rnd.Next(1, 25);
        int numbers = rnd.Next(0,9);
        int chars = rnd.Next(0, 25);
        string lettersAndChars = "";
        Console.SetCursorPosition(row, col);
        int choose = rnd.Next(1,3);
        while (choose == 1)
        {        
            lettersAndChars += (int)(numbers); 
            break;
        }
        while (choose == 2)
        {
            lettersAndChars += (char)(chars + 'A');
            break;
        }
        lettersAndChars = lettersAndChars.ToUpper();
        Console.WriteLine(lettersAndChars);
        DateTime endTime = DateTime.Now.AddSeconds(2);      
        while (!Console.KeyAvailable && DateTime.Now < endTime)
            Thread.Sleep(1);
        if (Console.KeyAvailable)
        {
            var keyPress = Console.ReadKey();
            string keyPressString = keyPress.KeyChar.ToString();
            keyPressString = keyPressString.ToUpper();
            if (keyPressString == lettersAndChars)
            {
                Console.Clear();
                continue;
            }
        }

        Console.Clear();
        int leftOffSet = (Console.WindowWidth / 2) -3;
        int topOffSet = (Console.WindowHeight / 2) -2;
        Console.SetCursorPosition(leftOffSet, topOffSet);
        Console.WriteLine("Game Over");
        leftOffSet = (Console.WindowWidth / 2) - 25;
        topOffSet = (Console.WindowHeight / 2);
        Console.SetCursorPosition(leftOffSet, topOffSet);
        Console.WriteLine("Press \"R\" to start again or \"ESC\" to exit the game...");
        var resOrExit = Console.ReadKey();
        Console.Clear();
        if ((char)resOrExit.Key == 'R')
        {
            continue;
        }
        else if (resOrExit.Key == ConsoleKey.Escape)
        {
            Environment.Exit(0);
        }
        }

    }
    }