C# 求交错数组中的值的和

C# 求交错数组中的值的和,c#,.net,sum,jagged-arrays,C#,.net,Sum,Jagged Arrays,我有一个锯齿状数组用于获取学生和科目详细信息,我使用了两个锯齿状数组,一个用于学生详细信息(姓名和rollno),另一个用于科目详细信息(科目和分数),现在我想总结特定学生的所有分数,有人能告诉我怎么做吗,下面是我的全部代码 public void GetStudentDetails() { Console.WriteLine("Please enter number of Students"); int STUDENTS = C

我有一个锯齿状数组用于获取学生和科目详细信息,我使用了两个锯齿状数组,一个用于学生详细信息(姓名和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");
                    }
                }
            }
        }
public void GetStudentDetails()
{
Console.WriteLine(“请输入学生人数”);
int STUDENTS=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“请输入主题数量”);
int SUBJECTS=Convert.ToInt32(Console.ReadLine());//0是为学生名保留的,因此标记从索引1开始,因此如果将标记设置为3,它将要求2个标记
字符串[][]学生详细信息=新字符串[学生][];
字符串[][]主题\详细信息=新字符串[主题][];
字符串[][]结果_详细信息=新字符串[学生][];
StringBuilder stud_sub_info=新StringBuilder();
stud_sub_info.Append(“Name\tRoll No\t”);
for(int a=0;a
使用Linq、集合和创建类,而不是锯齿状数组,这样做会容易得多。这将允许您将一名学生链接到多个课程,并且在每个课程中都有许多作业。然后是Linq语句,例如:

Sum(x => x.StudentGrade)
以下是我刚开始时的存根副本,目的是了解我可以如何处理总和和其他信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagement {
  public class Student {

    public static void Main(string[] args) {
      List<Student> students = new List<Student>();

      Console.Write("Enter number of students: ");
      int numStudents = int.Parse(Console.ReadLine());

      for (int i = 0; i < numStudents; i++) {
        Student s = new Student();
        students.Add(s);
      }

      Console.Write("Press any key to continue...");
      Console.ReadKey(true);
    }

    public string FirstName { get; set; }
    public List<Course> Courses { get; set; }

    public Student() {
      this.Courses = new List<Course>();

      Console.Write("Enter First Name: ");
      this.FirstName = Console.ReadLine();

      Console.Write("Enter in number of courses: ");
      int numCourses = int.Parse(Console.ReadLine());

      for (int i = 0; i < numCourses; i++) {
        Course course = new Course();
        this.Courses.Add(course);
      }

    }
  }

  public class Course {
    public string Title { get; set; }
    public string Code { get; set; }
    public string Section { get; set; }
    public List<Assignment> Assignments { get; set; }

    public Course() {
      this.Assignments = new List<Assignment>();

      // Set Title, Code, Section
      Console.Write("Enter nuber of assignments: ");
      int numAssignments = int.Parse(Console.ReadLine());

      for ( int i = 0; i < numAssignments; i++) {
        Assignment assignment = new Assignment();
        this.Assignments.Add(assignment);
      }

    }
  }

  public class Assignment {
    public string Name { get; set; }
    public double MaxGrade { get; set; }
    public double StudentGrade { get; set; }

    public Assignment() {
      // Prompt for Name, MaxGrade, StudentGrade
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间学生管理{
公立班学生{
公共静态void Main(字符串[]args){
列出学生=新建列表();
控制台。写(“输入学生人数:”);
int numStudents=int.Parse(Console.ReadLine());
对于(int i=0;i
我当然不会使用这种锯齿状数组。这使得编码变得困难

例如,在您的代码中,您正在捕获每个学生的科目分数,但当您捕获时,您将删除前面学生的数据。
subject\u details
交错数组没有学生索引。您在运行时有效地覆盖了数据。您需要将数组声明为
string[][]

这是您的代码,简化了,可以使用:

string[][] student_details = new string[STUDENTS][];
string[][][] subject_details = new string[STUDENTS][][];
string[][] result_details = new string[STUDENTS][];

for (int a = 0; a < STUDENTS; a++)
{
    student_details[a] = new string[STUDENTS * 2];
    subject_details[a] = new string[SUBJECTS][];
    Console.WriteLine("Enter Student {0} {1}:", a + 1, "Name");
    student_details[a][0] = Console.ReadLine();
    Console.WriteLine("Enter Student {0} {1}:", a + 1, "Roll-No");
    student_details[a][1] = Console.ReadLine();
    for (int c = 0; c < SUBJECTS; c++)
    {
        subject_details[a][c] = new string[SUBJECTS * 2];
        Console.WriteLine("Enter Name for Subject {0}", c + 1);
        subject_details[a][c][0] = Console.ReadLine();
        Console.WriteLine("Enter Marks for {0}", subject_details[a][c][0]);
        subject_details[a][c][1] = Console.ReadLine();
    }
}
for (int a = 0; a < STUDENTS; a++)
{
    Console.Write(student_details[a][0] + "\t");
    Console.Write(student_details[a][1] + "\t");
    for (int c = 0; c < SUBJECTS; c++)
    {
        Console.Write(subject_details[a][c][0] + "\t");
        Console.Write(subject_details[a][c][1] + "\t");
    }
}
这给了我一个如下的结构:

现在,您可以使用此查询获取总分:

var results =
    query
        .Select(x => new
        {
            x.Name,
            TotalMarks = x.Subjects.Sum(y => y.Marks)
        });
现在看起来像:


我完全同意,我花了5分钟理解了大量的数组代码,又花了5分钟来决定我是否有足够的精力来编写一个建设性的答案+有趣的是,我的学生两周前在课堂上做了这个作业,当时我们正在做《所有权》
var results =
    query
        .Select(x => new
        {
            x.Name,
            TotalMarks = x.Subjects.Sum(y => y.Marks)
        });