结构数组示例 [英] Array of structs example
问题描述
我对C#中的结构有点陌生.
i am bit new to structs in c#..
我的问题是
编写一个控制台应用程序,以接收一组学生的以下信息: 学生编号,学生姓名,课程名称,出生日期. 该应用程序还应该能够显示所输入的信息. 使用结构来实现.
Write a console application that receives the following information for a set of students: studentid, studentname, coursename, date-of-birth.. The application should also be able to display the information being entered.. Implement this using structs..
我直到这为止->
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
结构:
You've started right - now you just need to fill the each student
structure in the array:
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名学生.
Now, just iterate once again and write these information to the console. I will let you do that, and I will let you try to make program to take any number of students, and not just 4.
这篇关于结构数组示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!