Console.ReadLine(); 会等待直到用户按下回车,一次读入一行
Console.ReadKey(); 则是等待用户按下任意键,一次读入一个字符。
class Program
{
private List<Student> allStu = new List<Student>();
string yesORno;
bool flag = true;
public void AddStudent()
{
while (flag)
{
Student s = new Student();
Console.WriteLine("请输入学生姓名:");
s.StuName = Console.ReadLine();
Console.WriteLine("请输入学生学号");
s.StuNum = Console.ReadLine();
allStu.Add(s);
Console.WriteLine("是否继续输入:请输入y或者n");
yesORno = Console.ReadKey().Key.ToString();
switch (yesORno)
{
case "Y":
flag = true;
break;
case "N":
flag = false;
break;
}
}
}
public void ShowStu()
{
for (int i = 0; i < allStu.Count; i++)
{
Console.WriteLine(allStu[i].ToString());
}
}
static void Main(string[] args)
{
Program p = new Program();
p.AddStudent();
p.ShowStu();
Console.ReadLine();
}
}
class Student
{
private string stuName;
public string StuName
{
get { return stuName; }
set { stuName = value; }
}
private string stuNum;
public string StuNum
{
get { return stuNum; }
set { stuNum = value; }
}
public override string ToString()
{
return "姓名" + stuName + "......." + "学号" + stuNum;
}
}