【问题标题】:How to get the first character如何获得第一个字符
【发布时间】:2012-07-12 14:20:23
【问题描述】:

首先,Console.ReadKey() 不是答案。

我需要能够擦除我写的第一个字符。

基本上,我想要做的是一个测量你打字速度的应用程序。 它工作得很好,但我正在努力解决美学问题。

我一开始是调用Console.ReadKey(),然后调用这个启动计时器的函数(好吧,我在编写应用程序30分钟后意识到恰好有一个秒表类._.),然后我将存储用户使用Console.ReadLine() 输入的字符串,然后停止此计时器。

static Word[] getWords()
{
    Word[] words;
    int c = Console.ReadKey().KeyChar;
    Timing.start();
    string str = (c.ToString() + Console.ReadLine()).ToLower();
    charCount = str.Length;
    words = Word.process(str);
    Timing.stop();
    return words;
}

charCount 是一个静态 int,Word 只是一个包装字符串的类。 Word.process 是一个函数,它接受一个字符串,省略所有符号并返回用户键入的单词数组。 Timing 只是一个包装 Timer 类的类。 我认为这就是代码需要解释的全部内容。

当用户键入一个字符并且他必须能够擦除它时,我需要做的是调用Timing.start()

【问题讨论】:

  • 必须是控制台应用程序吗?因为如果你被允许使用 Windows 窗体,那真的不是适合这项工作的工具......
  • google“基于事件的键盘输入”
  • 为什么Console.ReadKey() 不是答案?因为你需要能够使用退格?您是否有任何理由不能使用 Console.ReadKey() 并自己处理退格功能?
  • @minitech:它仍将是一个控制台应用程序。对此感到抱歉:/
  • @AllonGuralnek:嗯,我可以。这实际上是我最后的手段。

标签: c# .net input console.readkey


【解决方案1】:

需要一些调整,但是这样的东西怎么样?

更新 - 现在一 (1) 个退格有效,但多个退格无效。啊!希望这会为您指明正确的方向。

更新 #2 - 使用 StringBuilder.... 退格现在可以使用 :)

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder();
            // you have alot of control on cursor position using
            // Console.SetCursorPosition(0, Console.CursorTop -1);
            List<DateTime> inputs = new List<DateTime>();
            ConsoleKeyInfo cki;

            Console.WriteLine("Start Typing...");
            Console.WriteLine("Press the Escape (Esc) key to quit: \n");
            do
            {
                cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.Spacebar)
                {
                    sb.Append(cki.KeyChar);
                }

                else if (cki.Key == ConsoleKey.Backspace)
                {
                    Console.Write(" ");
                    Console.Write("\b");
                    sb.Remove(sb.Length - 1, 1);
                }

                else if (cki.Key == ConsoleKey.Enter)
                {
                    sb.Append(cki.KeyChar + " ");
                    Console.WriteLine("");
                }

                else
                {
                    sb.Append(cki.KeyChar);
                }

                inputs.Add(DateTime.Now);
            } while (cki.Key != ConsoleKey.Escape);

            Console.WriteLine("");
            Console.WriteLine("=====================");
            Console.WriteLine("Word count: " + Regex.Matches(sb.ToString(), @"[A-Za-z0-9]+").Count);

            TimeSpan duration = inputs[inputs.Count - 1] - inputs[0];

            Console.WriteLine("Duration (secs): " + duration.Seconds);
            Console.ReadLine();
        }
    }
}

【讨论】:

  • 我非常感谢您的努力,它似乎可以正常工作,但是当我尝试擦除字符时,控制台只会更改我输入的位置,而不会实际删除文本。编辑:再一次,我也许可以检查是否单击了退格按钮,清除屏幕并重新显示字符,但我觉得这样做很糟糕,尤其是当控制台中有很多文本时。
  • 哦,你更新了。我想回答我自己的问题,但似乎我的声誉不足。我有解决办法。我需要使用 Console.KeyAvailable 检查输入流中是否有任何字符。
  • 我已将其更新为使用 StringBuilder 记录输入,然后在最后使用 Regex 计算单词。希望这会有所帮助:)
猜你喜欢
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2017-05-26
相关资源
最近更新 更多