【发布时间】: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