我有一个用于获取学生和主题细节的锯齿状数组,我使用了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");
}
}
}
}
答案 0 :(得分: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
}
}
}
答案 1 :(得分: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查询将锯齿状阵列变为可行的东西。 / p>
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)
});
现在看起来像: