必须使用string类型的变量来接收.Console.ReadLine();的值.
练习:
1.问别人喜欢吃什么水果,假如用户输入"苹果"则显示哈哈,这么巧,我也喜欢苹果;
namespace _10.接收用户输入_01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请问你喜欢吃什么水果?");
string fruits = Console.ReadLine();
if (fruits == "苹果")
{
Console.WriteLine("哈哈我也喜欢吃{0}",fruits);
}
Console.ReadKey();
}
}
}
2.请用户输入年龄,性别,当用户按下某个按键后,在屏幕上显示:您好:xxx您的年龄是xx.
namespace _10.接收用户输入02
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入您的姓名:");
string name = Console.ReadLine();
Console.WriteLine("请输入您的年龄:");
int age;
try
{
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(name + ":您好,您的年龄是:" + age);
}
catch
{
Console.WriteLine("年龄输入不正确.");
}
Console.ReadKey();
}
}
}