结构数组示例

我对c#中的结构有点新意。

我的问题是:

编写一个控制台应用程序,它接收一组学生的以下信息:studentid,studentname,coursename,birth-of-birth ..应用程序还应该能够显示正在输入的信息。使用结构实现此…

我已经到了这个 – >

struct student { public int s_id; public String s_name, c_name, dob; } class Program { static void Main(string[] args) { Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); s_id = Console.ReadLine(); s_name = Console.ReadLine(); c_name = Console.ReadLine(); s_dob = Console.ReadLine(); student[] arr = new student[4]; } } 

请在此之后帮助我..

你已经开始了 – 现在你只需要填充数组中的每个student结构:

 struct student { public int s_id; public String s_name, c_name, dob; } class Program { static void Main(string[] args) { student[] arr = new student[4]; for(int i = 0; i < 4; i++) { Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); arr[i].s_id = Int32.Parse(Console.ReadLine()); arr[i].s_name = Console.ReadLine(); arr[i].c_name = Console.ReadLine(); arr[i].s_dob = Console.ReadLine(); } } } 

现在,再次迭代并将这些信息写入控制台。 我会让你这样做,我会让你尝试制定计划来吸引任何数量的学生,而不仅仅是4。

给定结构的实例,您可以设置值。

  student thisStudent; Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth"); thisStudent.s_id = int.Parse(Console.ReadLine()); thisStudent.s_name = Console.ReadLine(); thisStudent.c_name = Console.ReadLine(); thisStudent.s_dob = Console.ReadLine(); 

请注意,此代码非常脆弱,因为我们根本不检查用户的输入。 并且您不清楚用户是否希望在单独的行中输入每个数据点。