c#数组需要帮助

本文关键字:帮助 数组 | 更新日期: 2023-09-27 18:04:51

我是c#编程的初学者,我的讲师给了我们一个棘手的项目。我都完成了,除了……数组!

长话短说,我有5个文本框,所有的输入从用户。这些信息将被存储到一个数组中,然后按顺序列出(出生日期顺序),显示在一个富文本框中,我在下面列出了我设法完成的代码:

private void button2_Click(object sender, EventArgs e)
{
   {
      bc[0] = new Student();
      bc[1] = new Student(Convert.ToInt32(textBox1.Text), "Mary", "Ford");
      bc[2] = new Student(1254, "Andrew", "White");
      bc[3] = new Student(1256, "Liam", "Sharp", " ");
      bc[4] = new Student(1266, "Michael", "Brown", " ");
      for (int i = 0; i < 5; i++)
      {
         string bcString = bc[i].studentToString() + "'r'n"; 
         richTextBox1.AppendText(bcString);
      }
   }
}    
CLASS "Student":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_2
{
    class Student
    {
        private int accountNum;
        private string firstName;
        private string lastName;
        private string balance;
        // first constructor
        public Student()
        {
            accountNum = 0;
            firstName = "";
            lastName = "";
            balance = "";
        }
        // second constructor
        public Student(int accValue, string firstNameVal, string lastNameVal)
        {
            accountNum = accValue;
            firstName = firstNameVal;
            lastName = lastNameVal;
            balance = "";
        }
        // third constructor
        public Student(int accValue, string firstNameVal,
                             string lastNameVal, string balanceValue)
        {
            accountNum = accValue;
            firstName = firstNameVal;
            lastName = lastNameVal;
            balance = balanceValue;
        }
        public int AccountNum
        {
            get
            {
                return accountNum;
            }
            set
            {
                accountNum = value;
            }
        }
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }
        public string studentToString()
        {
            return (Convert.ToString(accountNum) + " " + firstName +
                    " " + lastName + " " + balance);
        }
    }
}

c#数组需要帮助

让您的类Student实现IComparable接口,然后对字段DateOfBirth(如果存在)进行排序。这个例子在AccountNum上工作,但是用DateOfBirth

更改应该是微不足道的。
Student[] bc = new Student[5];
bc[0] = new Student();
bc[1] = new Student(9999, "Mary", "Ford");
bc[2] = new Student(1254, "Andrew", "White");
bc[3] = new Student(1256, "Liam", "Sharp", " ");
bc[4] = new Student(1266, "Michael", "Brown", " ");
// Here the sort on the AccountNum
Array.Sort(bc);
// A StringBuilder instead of the RichTextBox for testing....    
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++)
{
    string bcString = bc[i].studentToString() + "'r'n"; 
    sb.Append(bcString);
}
Console.WriteLine(sb.ToString());

学生:(只是比较的部分)

class Student : IComparable
{
    .....

    public int CompareTo(object obj) 
    {
        if (obj == null) return 1;
        Student otherStudent = obj as Student;
        if (otherStudent != null) 
            return this.accountNum.CompareTo(otherStudent.AccountNum);
        else 
        throw new ArgumentException("Object is not a Student");
    }
    ....
}