为了学习,我最近一直在玩游戏,并且想在最近的一个程序上寻求帮助。在这个课程中,我有学生和考试。每个学生都有一个测试,我需要两个匹配。我以为我可以创建一个学生阵列,然后在阵列中进行测试,直到找到匹配为止,但老实说我真的很难过这样做。
此程序的目的是将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;
}
}
任何指导都将不胜感激。谢谢!
答案 0 :(得分:1)
您可以使用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; });
}
现在每个“学生”收集的学生都应该有相应的考试成绩。
答案 1 :(得分:0)
if(currentIndex[object] == Student)
正确
if(object[currentIndex] == Student)
而且,学生是什么?在“==学生”中,如果你宣布只是课程,那么它将等于最后宣布的学生。
答案 2 :(得分:0)
为什么不使用LINQ对名称执行连接操作。有些东西:
students.Join(测试,x =&gt; x.Name,y =&gt; y.Name,(x,y)=&gt; new {Student = x,Test = y});
如果您想对特定学生的所有考试进行分组,请转到GroupJoin。
答案 3 :(得分:0)
这会奏效。但使用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;
}
}
答案 4 :(得分:0)
如果我理解了您的问题,那么我认为您需要将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
,实际上并不推荐,但它是您示例中两个类之间唯一的共同点。