-1

我有一个用于获取学生和主题详细信息的锯齿状数组,我使用了 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");
                    }
                }
            }
        }
4

2 回答 2

2

使用 Linq、Collections 和创建类而不是锯齿状数组会更容易做到这一点。这将允许您将 1 个学生链接到许多课程,并且在每个课程中都有许多作业。然后是 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
    }
  }
}
于 2012-12-04T21:40:26.017 回答
1

我当然不会使用这样的锯齿状数组。它使编码变得困难。

例如,在您的代码中,您正在捕获每个学生的科目分数,但是当您这样做时,您会清除以前学生的数据。subject_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");
    }
}

现在,鉴于我们忽略了每个学生必须拥有相同数量的科目并且您允许科目分数的非数字输入这一事实,您可以使用以下 LINQ 查询将锯齿状数组转换为可行的数组。

var query =
    from i in Enumerable.Range(0, STUDENTS)
    let Name = student_details[i][0]
    let RollNo = student_details[i][1]
    let Subjects = (
        from j in Enumerable.Range(0, SUBJECTS)
        let Subject = subject_details[i][j][0]
        let Marks = int.Parse(subject_details[i][j][1])
        select new { Subject, Marks }
    )
    select new { Name, RollNo, Subjects };

这给了我一个看起来像这样的结构:

查询数据结构

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

var results =
    query
        .Select(x => new
        {
            x.Name,
            TotalMarks = x.Subjects.Sum(y => y.Marks)
        });

现在看起来像:

总分

于 2012-12-05T03:56:27.887 回答