基本代码(第一次编码)错误(visual studio 2015 for desktop express)

Rab*_*mcv -2 c#

        Console.WriteLine("Type a number, Any number!");
        ConsoleKeyInfo KeyInfo = Console.ReadKey();

        if (KeyInfo.KeyChar -- 'a')
        {
            Console.WriteLine("Thats not a number, Knock it off!");
        }

        Console.WriteLine("Did you press {0}", KeyInfo.KeyChar.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

我在上面的代码中遇到以下错误,我该如何修复这些错误?

CS1026 ) expected
CS1002 ; expected
CS0200 Property or Indexer 'ConsoleKeyInfo.KeyChar' Cannot be assigned to -- it is read only
CS1513 } expected
Run Code Online (Sandbox Code Playgroud)

Sac*_*hin 5

ConsoleKeyInfo.KeyChar 获取当前ConsoleKeyInfo对象表示的Unicode字符.所以它是一个只读属性,这意味着你不能修改这个属性的值.

通过查看代码,您似乎想要检查a您的if条件中的角色.因此,对于刚刚使用比较操作符(等于运算符)==这样的

if (KeyInfo.KeyChar == 'a')
Run Code Online (Sandbox Code Playgroud)