关与C#.net

来源:百度知道 编辑:UC知道 时间:2024/06/05 15:39:21
using System;
class hello
{
public static void Main(string[] args)
{
Console.WriteLine("请选择要显示的内容");
Console.WriteLine("1.开发环境");
Console.WriteLine("2.编程语言");
Console.WriteLine("3.项目类型");
Console.WriteLine("4.退出");
ConsoleKeyInfo i = Console.ReadKey();
if (i == '1')
Console.WriteLine(".net");
else if (i.KeyChar=='2')
Console.WriteLine("C sharp");
else if (i.KeyChar=='3')
Console.WriteLine("控制台应用程序");

}
}
上面的代码运行时报错:hello.cs(13,13): error CS0019:
运算符“==”无法应用于“System.ConsoleKeyInfo”和“char”类型的操作数

请问是什么原因 我是照书上写的

ConsoleKeyInfo i = Console.ReadKey();
if (i.KeyChar == '1')
Console.WriteLine(".net");
else if (i.KeyChar=='2')
Console.WriteLine("C sharp");
else if (i.KeyChar=='3')

什么书 误人子弟呀
楼上正解
在给你个修改方法
改这一句

char i=Console.ReadKey().KeyChar;