【问题标题】:Array Of Objects Help! Compare Student Name with Test Name and assign score对象数组帮助!将学生姓名与考试姓名进行比较并分配分数
【发布时间】:2015-05-10 06:38:30
【问题描述】:

为了学习,我最近一直在玩游戏,想就我最近搞砸的一个程序寻求一些帮助。在这个程序中,我有学生和测试。每个学生都有一个测试,我需要将两者匹配。我以为我可以创建一个学生数组,然后通过数组推送测试,直到找到匹配项,但老实说,我很难做到这一点。

该程序的目的是匹配 Test nameOfStudent 和 Student name。然后取 Test scoreOfTest 的值并将其应用于学生分数。 例如 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;

    }
}

任何指导将不胜感激。谢谢!

【问题讨论】:

  • 您可以使用 Dictionary 进行研究,您可以在其中将学生对象映射到每个学生测试,如果您想要一个示例,请告诉我
  • 您对问题的解释不清楚...请输入您实际尝试做的...例如“比较学生姓名与测试姓名”?请正确描述。

标签: c# arrays object search match


【解决方案1】:
if(currentIndex[object] == Student)

改正

if(object[currentIndex] == Student)

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

【讨论】:

    【解决方案2】:

    为什么不使用 LINQ 对名称执行连接操作。类似于:

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

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

    【讨论】:

      【解决方案3】:

      您可以使用 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”集合中的每个学生都应该应用相应的考试成绩。

      【讨论】:

      • 感谢您的帮助!
      【解决方案4】:

      这会奏效。但是使用 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; 
                  }
              }
      

      【讨论】:

        【解决方案5】:

        如果我理解您的问题,那么我认为您需要将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,实际上并不推荐,但它是您示例中两个类之间唯一共同的东西。

        【讨论】:

          猜你喜欢
          • 2013-08-15
          • 1970-01-01
          • 1970-01-01
          • 2014-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-31
          • 1970-01-01
          相关资源
          最近更新 更多