如何将我的名字和性别变量放入数组中

本文关键字:变量 数组 我的名字 | 更新日期: 2023-09-27 18:31:15

我正在尝试制作学生记录应用程序,我能够将姓名和性别与文本文件分开,但是当我尝试将它们放入数组中时,数组将始终显示为空。当我单独尝试时,addStudent 方法有效。

        StudentList list = new StudentList();
        string name = "";
        string gender = "";
        string line = "";
        char[] selected = line.ToCharArray();
        openFileDialog1.ShowDialog();
        string file = openFileDialog1.FileName;
        StreamReader sr = new StreamReader(file);

        while ((line = sr.ReadLine()) != null)
        { 
            studentList.Items.Add(line);
            for (int i = 0; i < selected.Length; i++)
            {
                if (selected[i] != '(')
                {
                    name += selected[i];
                }
                else if (selected[i] == '(')
                {
                    gender += selected[i + 1];
                    break;
                }
                Student student = new Student();
                student.setName(name);
                student.setGender(gender);
                list.addStudent(student);
            }
        }

这是我根据名称和性别对变量进行排序的另一个类(不假设更改此代码)

 class Student
{
    private string name;
    private string gender;
    private DateTime birthDate;
    public Student()
    {
        name = null;
        gender = null;
        birthDate = DateTime.Now; // default to set to current date
    }
    public void setName(string n)
    {
        name = n;
    }
    public string getName()
    {
        return name;
    }
    public void setGender(string g)
    {
        gender = g;
    }
    public string getGender()
    {
        return gender;
    }
    public void setBirthDate(DateTime bd)
    {
        birthDate = bd;
    }
    public DateTime getBirthDate()
    {
        return birthDate;
    }
}
class StudentList
{
    private Student[] list;
    private int numStudents;
    private int maxNum;
    public StudentList()
    {
        maxNum = 300;
        list = new Student[maxNum];   
        numStudents = 0;
    }
    public Student getStudent(int index)
    {
        Student ret = null;
        try
        {
            ret = list[index];
        }
        catch (Exception ex)
        {
             MessageBox.Show("Exception has occurred: " + ex.Message);
        }
        return ret;
    }
    public bool addStudent(Student s)
    {
        bool ret = false;
        if (numStudents < maxNum-1 )
        {
            list[numStudents] = s;
            numStudents++;
            ret = true;
        }
        else
        {
            MessageBox.Show("Maximum Students exceeded");
        }
        return ret;
    }
    public int getNumStudents()
    {
        return numStudents;
    }
    public void clear()
    {
        for (int i = 0; i < numStudents; i++)
            list[i] = null; // set to null
        numStudents = 0;
    }
    public Student findStudent(string name)
    {
        Student ret = null;
        for (int i = 0; i < numStudents; i++ )
        {
            Student currentStudent = list[i];
            if (currentStudent.getName() == name)
            {
                ret = currentStudent;
                break;
            }
        }
        return ret;
    }
}

如何将我的名字和性别变量放入数组中

你需要把string name = "";string gender = "";selected = line.ToCharArray();放在while循环中。此外,还应在for循环之外创建Student对象。

学生列表列表 = 新学生列表();

openFileDialog1.ShowDialog();
string file = openFileDialog1.FileName;
StreamReader sr = new StreamReader(file);
string line = "";
while ((line = sr.ReadLine()) != null)
{ 
    string name = "";
    string gender = "";
    char[] selected = line.ToCharArray();
    for (int i = 0; i < selected.Length; i++)
    {
        if (selected[i] != '(')
        {
            name += selected[i];
        }
        else if (selected[i] == '(')
        {
            gender += selected[i + 1];
            break;
        }
    }
    Student student = new Student();
    student.setName(name);
    student.setGender(gender);
    list.addStudent(student);
    //Add student objet rather than line string
    studentList.Items.Add(student);
}

更新根据您的评论,我认为您应该覆盖 Studend classToString()方法

public override string ToString()
{
   return name + "("+ gender+")";
}

这样的事情应该这样做(我没有编译它,所以可能会出现小错误):

    StudentList list = new StudentList();
    string line = "";
    openFileDialog1.ShowDialog();
    string file = openFileDialog1.FileName;
    StreamReader sr = new StreamReader(file);
    while ((line = sr.ReadLine()) != null)
    { 
        string[] lineStudents = line.Split(')');
        foreach(string lineStudent in lineStudents)
        {
            Student student = new Student();
            string[] studentProps = lineStudent.Split('(');
            student.setGender(studentProps[1]);
            student.setName(studentProps[0].Trim());
            list.addStudent(student);
        }
    }