【问题标题】:How to count seconds and then use them in a conditional?如何计算秒数然后在条件中使用它们?
【发布时间】:2014-03-24 11:44:22
【问题描述】:

我是一个新手程序员,我已经尝试处理这个问题超过 2 个小时了。我希望程序做的是计算秒数,如果它超过第二秒(什么?),做一些事情,例如显示“游戏结束”或类似的东西。问题是,在这 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");
        }


    }
}

【问题讨论】:

  • 使用计时器将秒数(每 1000 毫秒)计入静态变量,然后使用它。
  • 您只检查按下按钮时的结束时间。您是否希望它在两秒钟后采取一些行动而不是等待输入?
  • @Chris 计时器解决方案也可以做到这一点:)
  • @Ilan321:那是针对 OP 的。
  • 是的,@Chris,这就是我想要的。我想为家里不擅长在键盘上查找字母的人创建这个程序。

标签: c# seconds


【解决方案1】:

我不会使用计时器。您可以执行以下操作:

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");

【讨论】:

    【解决方案2】:

    @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);
            }
            }
    
        }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多