我有一個鋸齒陣列來獲取學生和主題的詳細信息,我使用了2個鋸齒陣列,一個用於學生的詳細信息(名稱和rollno),另一個用於主題的詳細信息(主題和標記),現在我想總結一下特定學生的所有標記,誰能告訴我怎麼去解決這個問題,下面是我的整個代碼。在鋸齒陣列中總結數值
public void GetStudentDetails()
{
Console.WriteLine("Please enter number of Students");
int STUDENTS = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter number of Subjects");
int SUBJECTS = Convert.ToInt32(Console.ReadLine()); //0 is reserved for the student name, so the marks starts from the index 1, hence if you set the marks for 3, it will ask for 2 marks
string[][] student_details = new string[STUDENTS][];
string[][] subject_details = new string[SUBJECTS][];
string[][] result_details = new string[STUDENTS][];
StringBuilder stud_sub_info = new StringBuilder();
stud_sub_info.Append("Name\tRoll-No\t");
for (int a = 0; a < STUDENTS; a++)
{
student_details[a] = new string[STUDENTS * 2];
for (int b = 0; b < 2; b++)
{
Console.WriteLine("Enter Student {0} {1}:", a + 1, (b == 0) ? "Name" : "Roll-No");
student_details[a][b] = Console.ReadLine();
}
for (int c = 0; c < SUBJECTS; c++)
{
subject_details[c] = new string[SUBJECTS * 2];
for (int d = 0; d < 2; d++)
{
if (d == 0)
{
Console.WriteLine("Enter Name for Subject {0}", c + 1);
subject_details[c][d] = Console.ReadLine();
stud_sub_info.Append(subject_details[c][d] + "\t");
}
else
{
Console.WriteLine("Enter Marks for {0}", subject_details[c][d - 1]);
subject_details[c][d] = Console.ReadLine();
}
}
}
}
Console.WriteLine(stud_sub_info.ToString());
for (int a = 0; a < STUDENTS; a++)
{
for (int b = 0; b < 2; b++)
{
Console.Write(student_details[a][b] + "\t");
}
for (int c = 0; c < SUBJECTS; c++)
{
for (int d = 0; d < 2; d++)
{
Console.Write(subject_details[c][d] + "\t");
}
}
}
}
儘管需要稍微修改student_details [STUDENT * 2]和subject_details [SUBJECT * 2],在這兩種情況下(* 2)都不是必需的,因爲它會在數組中創建空白索引,所有其他代碼都會休眠很好,最好的部分是它的整潔,然後我的 – Abbas