为什么控制台会为我的数组中的元素写入相同的输出?

时间:2015-04-27 21:29:08

标签: c# arrays

我是C#编程的新手。我试图创建新对象(在我的情况下为StudentInfo对象)并将它们放在数组studentArr中。

数组初始化为length = 5,我想创建五个StudentInfo对象并将它们添加到数组中。我根本不认为这样做,我不知道我哪里出错了。

我应该通过Console.Readline为五个不同的学生输入学生信息,并且只打印出第一个学生信息。

我正在使用struct来访问学生信息。

非常感谢一些帮助!

static void Main(string[] args)
{
    StudentInfo[] studentArr = new StudentInfo[5];

    int objectNumber = 0;
    for (int i = 0; i < studentArr.Length; i++)
    {
        studentArr[i] = new StudentInfo();

        Console.WriteLine("Please enter StudentId, StudentName, CourseName, Date-Of-Birth");

        studentArr[i].firstName = Console.ReadLine();
        studentArr[i].lastName = Console.ReadLine();
        studentArr[i].birthDay = Console.ReadLine();
        studentArr[i].studentID = Console.ReadLine();
        studentArr[i].addressLine1 = Console.ReadLine();
        studentArr[i].addressLine2 = Console.ReadLine();
        studentArr[i].city = Console.ReadLine();
        studentArr[i].state = Console.ReadLine();
        studentArr[i].zipCode = Console.ReadLine();
        studentArr[i].country = Console.ReadLine();

        objectNumber = i + 1;
    }

    for (int i = 0; i < studentArr.Length; i++)
    {
        Console.WriteLine(
            "{0} {1} was born on {2}. Student ID: {3}", 
            studentArr[0].firstName, studentArr[0].lastName, 
            studentArr[0].birthDay, studentArr[0].studentID);

        Console.WriteLine(
            "Address: {0} {1} \n\t {2} {3} {4} {5}.", 
            studentArr[0].addressLine1, studentArr[0].addressLine2, 
            studentArr[0].city, studentArr[0].state, studentArr[0].zipCode, 
            studentArr[0].country);
    }
}

public struct StudentInfo
{
    public string firstName;
    public string lastName;
    public string birthDay;
    public string studentID;
    public string addressLine1;
    public string addressLine2;
    public string city;
    public string state;
    public string zipCode;
    public string country;

    // Constructor for StudentInfo
    public StudentInfo(
        string first, string last, string dob, string ID, 
        string address1, string address2, string city, 
        string state, string zip, string country)
    {
        this.firstName = first;
        this.lastName = last;
        this.birthDay = dob;
        this.studentID = ID;
        this.addressLine1 = address1;
        this.addressLine2 = address2;
        this.city = city;
        this.state = state;
        this.zipCode = zip;
        this.country = country;
    }
}

1 个答案:

答案 0 :(得分:3)

通过编写studentArr [0],您只打印第一个元素为0。它应该是studentArr [i]

for (int i = 0; i < studentArr.Length; i++)
{

    Console.WriteLine("{0} {1} was born on {2}. Student ID: {3}", studentArr[i].firstName, studentArr[i].lastName, studentArr[i].birthDay, studentArr[i].studentID);
    Console.WriteLine("Address: {0} {1} \n\t {2} {3} {4} {5}.", studentArr[i].addressLine1, studentArr[i].addressLine2, studentArr[i].city, studentArr[i].state, studentArr[i].zipCode, studentArr[i].country);
}