对象数组帮助!将学生姓名与测试名称进行比较并分配分数

Array Of Objects Help! Compare Student Name with Test Name and assign score

最近为了学习一直在玩,想请教一下最近搞的一个程序。在这个程序中,我有学生和测试。每个学生都有一个测试,我需要两个匹配。我想我可以创建一个学生数组,然后通过数组推送测试直到找到匹配项,但老实说我不知道​​如何去做。

该程序的目的是将 Test nameOfStudent 与学生姓名相匹配。然后取 Test scoreOfTest 的值并将其应用于 Student score。 例如 student1.score = test1.scoreOftest

using System.IO;
using System;

class Program
{
    static void Main ()
    {

下面我为我的学生和测试创建对象类

        Student student1 = new Student("Finn",0);
        Test test1 = new Test("Finn",100);

        Student student2 = new Student("AJ",0);
        Test test2 = new Test("AJ",97);

        Student student3 = new Student("Sami",0);
        Test test3 = new Test("Sami",80);

        Student student4 = new Student("John",0);
        Test test4 = new Test("John",72);

        Student student5 = new Student("Rey",0);
        Test test5 = new Test("Rey",61);

此字符串的目的是保存测试中的姓名(在本例中为 "Finn"),并将其与学生姓名相匹配。我还创建了一个学生数组,用于通过

发送的测试
string nameOnTest = test1.nameOfStudent;

        object[] arrayOfStudents = {student1, student2, student3, student4, student5};

我想如果我可以创建一个 currentIndex,我可以按照 if(currentIndex[i.name] == nameOnTest) 然后分配分数并停止循环

    for(int i = 0; i < arrayOfStudents.Length;i++){
        int currentIndex = i;
        if(currentIndex[object.name] == nameOnTest){
           //then do something similar to
         //  object.score = scoreOfTest   }
       }
    }
}

public class Test {

    public string nameOfStudent;
    public int scoreOfTest;

    public Test(string nameOfStudent, int scoreOfTest){

        this.nameOfStudent = nameOfStudent;
        this.scoreOfTest = scoreOfTest;

    }
}

public class Student {

  public  string name;
  public  int score;

    public Student(string name, int score){

        this.name = name;
        this.score = score;

    }
}

如有任何指导,我们将不胜感激。谢谢!

if(currentIndex[object] == Student)

更正为

if(object[currentIndex] == Student)

还有,什么是学生?在“== Student”中,如果您只声明 class,那么它将等于最后声明的学生。

为什么不使用LINQ 对名称执行连接操作。内容如下:

students.Join(Tests, x => x.Name, y => y.Name, (x, y) => new {Student = x, Test = y});

如果您想对特定学生的所有测试进行分组,请使用 GroupJoin。

您可以使用 Linq 简化代码

using System.Linq;
...
...
static void Main(string[] args)
{
    List<Student> students = new List<Student>()
                                 {
                                     new Student("Finn", 0),
                                     new Student("AJ", 0),
                                     new Student("Sami", 0),
                                     new Student("John", 0),
                                     new Student("Rey", 0)
                                 };

    List<Test> tests = new List<Test>()
                                 {
                                     new Test("Finn", 100),
                                     new Test("AJ", 97),
                                     new Test("Sami", 80),
                                     new Test("John", 72),
                                     new Test("Rey", 61)
                                 };

    tests.ForEach(
        t =>
            { students.Where(s => s.name == t.nameOfStudent).FirstOrDefault().score = t.scoreOfTest; });
}

现在 "students" 集合中的每个学生都应该应用相应的测试分数。

这会奏效。但使用 Linq 会更好

        string nameOnTest = test1.nameOfStudent;
        Student[] arrayOfStudents = { student1, student2, student3, student4, student5 };
        for (int i = 0; i < arrayOfStudents.Length; i++)
        {
            int currentIndex = i;
            if (arrayOfStudents[i].name == nameOnTest)
            {
                arrayOfStudents[i].score = test1.scoreOfTest; 
            }
        }

如果我理解了你的问题,那么我认为你需要将 Student Class 中学生的分数与 Test Class 中相应的分数相匹配。

您可以使用 Linq Query Expressions(C# 3.0)。它给您很大的灵活性,也很容易阅读。

            List<Student> StudentsList = new List<Student>()
            {
                 new Student("Finn", 0),
                                 new Student("AJ", 0),
                                 new Student("Sami", 0),
                                 new Student("John", 0),
                                 new Student("Rey", 0)
            };

            List<Test> TestList = new List<Test>()
                             {
                                 new Test("Finn", 100),
                                 new Test("AJ", 97),
                                 new Test("Sami", 80),
                                 new Test("John", 72),
                                 new Test("Rey", 61)
                             };

            var results = from s in StudentsList
                          join t in TestList
                              on s.name equals t.nameOfStudent
                          orderby s.name
                          select new { Name = s.name, Score = t.scoreOfTest, };

            foreach (var v in results)
            {
                Console.WriteLine("Student ={0} has  Score={1}",
                v.Name, v.Score);
            }

它加入了 Name,这实际上是不推荐的,但它是您示例中 类 之间唯一的共同点。