3

我对 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();
        }
    }
4

6 回答 6

4

使用Console.ReadKey()而不是Console.Read().

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

要获得char接收者ReadKey,请使用:ch = Console.ReadKey().KeyChar;

于 2013-01-30T16:37:32.740 回答
0

使用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);

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

while (key != ConsoleKey.OemPeriod && key != ConsoleKey.Decimal);
于 2013-01-30T16:41:47.413 回答
0

如果您只想查看结果,则只需使用 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);
        }
    }
}
于 2013-01-30T16:42:07.900 回答
0

我的示例使用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();
    }
}
于 2013-01-30T16:42:14.157 回答
0

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

于 2013-01-30T16:38:35.963 回答
0

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

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

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

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

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

于 2013-01-30T16:48:46.723 回答