【问题标题】:How to read characters from the keyboard until a period is received, and count the number of spaces如何从键盘读取字符直到收到句号,并计算空格数
【发布时间】:2013-01-30 16:34:42
【问题描述】:

我对 C#、OOP 和 stackoverflow 非常陌生。这是我的第一个场景(几个问题)

我希望用户输入字符,直到收到句点 (.),然后计算并报告空格的数量。

我能做到这一点吗? (不确定你是否总是按回车/回车发送)

我可以不使用字符串来做到这一点吗? (我还没有涉及字符串,这是一个自学练习,因此我相信解决方案应该非常简单,但我得到了不寻常的结果。

我尝试了以下方法,但程序在我看到结果之前就关闭了,即使我在最后添加了一个Console.Read();,这通常可以工作......

class CountSpaces
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter characters,finish with a period (\".\"");
            char ch;
            int spaces=0;
            do
            {
                ch = (char)Console.Read();                
                if (ch == ' ')
                {                 
                    spaces++;
                }
            } while (ch != '.');

            Console.WriteLine("Number of spaces counted = {0}",spaces);
            Console.Read();
        }
    }

【问题讨论】:

  • 您应该做的一件事是熟悉 Visual Studio 调试器。网上有大量资源,但您可以尝试的第一件事是在您知道发生了什么的行上设置断点。您可以通过在该行上按 F9、单击 Debug>Toggle breakpoint 或单击左边距来执行此操作。您可以使用 F10 单步执行代码,并将鼠标悬停在变量上以查看程序中发生的情况

标签: c# console character


【解决方案1】:

使用Console.ReadKey() 代替Console.Read()

  • Console.ReadKey() 在您调用 Console.ReadKey() 之后返回是否按下了某个键。
  • Console.Read() 像在流中一样读取字符(在您的情况下根本没有用)。

要获取ReadKey 收到的char,请使用:ch = Console.ReadKey().KeyChar;

【讨论】:

    【解决方案2】:

    您需要ReadKey 而不是Read。后者只是读取流中存在的下一个字符,如果不存在则返回 -1,因为它不会等待(你可能没有那么快输入!)

    【讨论】:

      【解决方案3】:

      如果您只想查看结果,您只需使用 ReadKey:

      class CountSpaces
      {
          static void Main(string[] args)
          {
              Console.WriteLine("Enter characters,finish with a period (\".\"");
              char ch;
              int spaces = 0;
              do
              {
                  ch = (char)Console.Read();
                  if (ch == ' ')
                  {
                      spaces++;
                  }
              } while (ch != '.');
      
              Console.WriteLine("Number of spaces counted = {0}", spaces);
              Console.ReadKey();
          }
      }
      

      但是我认为如果您将程序更新为以下内容,它会变得更有趣:

      class Program
      {
          static void Main(string[] args)
          {
              while (true)
              {
                  Console.WriteLine("Enter characters,finish with a period (\".\"");
                  char ch;
                  int spaces = 0;
                  do
                  {
                      ch = (char)Console.Read();
                      if (ch == ' ')
                      {
                          spaces++;
                      }
                  } while (ch != '.');
      
                  Console.WriteLine("Number of spaces counted = {0}", spaces);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        我的示例使用Console.ReadKey 而不是Console.Read

        class Program
        {
            static void Main(string[] args)
            {
                int spaces = 0;
                char key;
                while ((key = Console.ReadKey().KeyChar) != '.') {
                    if (key == ' ')
                        spaces++;
                }
                Console.WriteLine();
                Console.WriteLine("Number of spaces: {0}", spaces);
                Console.ReadKey();
            }
        }
        

        【讨论】:

          【解决方案5】:

          如果我们创建一个从控制台创建一系列键的辅助方法:

          public static IEnumerable<char> ReadKeys()
          {
              while (true)
              {
                  yield return Console.ReadKey().KeyChar;
              }
          }
          

          它允许我们编写一个查询来准确描述您想要的内容:

          var spaces = ReadKeys()
              .TakeWhile(c => c != '.')
              .Count(c => c == ' ');
          

          取字符直到你得到一个句号,然后计算空格的数量。

          【讨论】:

          • 许多可能有效的说明如何实现这一点,我非常感激(并且惊喜!谢谢!)我想在我的脑海中进一步发展这个概念。 var 对我来说是新的(运行时类型声明?),我不太明白这将如何与其余部分相适应......你能提供这个想法的完整实现吗? Takewhile 和 Count 是类方法吗?
          【解决方案6】:

          使用Console.ReadKey() 获取ConsoleKeyInfo。然后验证按下Key。您不需要使用字符:

          int spaces = 0;
          ConsoleKey key;
          do
          {
              key = Console.ReadKey().Key;
              if (key == ConsoleKey.Spacebar)
                  spaces++;
          }
          while (key != ConsoleKey.OemPeriod);
          
          Console.WriteLine("Number of spaces counted = {0}",spaces);
          

          请记住,数字键盘上的句点键的值是 ConsoleKey.Decimal。因此,如果您需要处理两个句点键,则应编写以下条件:

          while (key != ConsoleKey.OemPeriod && key != ConsoleKey.Decimal);
          

          【讨论】:

            猜你喜欢
            • 2021-02-23
            • 1970-01-01
            • 2014-11-25
            • 1970-01-01
            • 2020-01-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-10
            相关资源
            最近更新 更多