如何计算每个科目的学生的位置? [英] how to calculate the position of students in each subject ?

查看:118
本文介绍了如何计算每个科目的学生的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份学生名单,该名单包含学生姓名,名单和英语,数学,医学等科目。现在我想计算每个学生在英语和其他科目的科目列表中的位置。



就像我在课堂上有45名学生,列表包含45名学生,每个学生我想计算他在相关科目中的位置。





I''ve a list of students, the list contains student name, roll number and subjects like english ,math, phy etc. Now i want to calculate the position of each student in the list for the subjects english and other subjects.

Like i have 45 students in class and list contains 45 students, for each student i want to calculate his position in the relevant subject.


List<Student> student = new List<Student>();







任何帮助。




Any to help.

推荐答案

使用OrderBy方法和foreach来设置Rank。



Use the OrderBy method and a foreach to set the Rank.

students.OrderByDescending(x=>x.English).ToList().ForEach(s=>s.PositionInEnglish=rank++)



etc ...


etc...


您可以使用Linq OrderBy方法:

You could use the Linq OrderBy method:
class student
    {
    public int marks;
    public student(int mark) { marks = mark; }
    }
void myButton_Click(object sender, EventArgs e)
    {
    List<student> list = new List<student>();
    list.Add(new student(7));
    list.Add(new student(3));
    list.Add(new student(5));
    list.Add(new student(88));
    list.Add(new student(2));
    list.Add(new student(963));
    list.Add(new student(6));
    list = list.OrderBy(s => s.marks).ToList();
    }


void Main()

{

List<学生> list = new List< student>();

list.Add(new Student(7,30)); //学生的位置= 3

list.Add(新学生(3,40)); //学生的位置= 6th

list.Add(新学生( 5,60)); //学生的位置= 5

list.Add(新学生(88,70)); //学生的位置= 2

list .Add(新学生(2,5)); //学生的位置= 7th

list.Add(新学生(963,90)); //学生的位置=第1名>
list.Add(新学生(6,34)); //学生的位置= 4

list.Add(新学生(963,343)); //学生的位置= 1st

list.Add(新学生(963,343)); //学生的位置= 1st



list.OrderByDescending(c => ; c.EngPts).ThenByDescending(c => c.MathPts);

//此时您的列表应按照这样排序

/ *

963 343

963 343

963 90

88 70

7 30
6 34

5 60
3 40

2 5

这似乎与你想要获得的排名相符

* /

}



班级学生

{

//公共字符串positionInEnglish; //为每个学生计算并放置在此字段中的位置

//公共字符串positionInMath; //要计算的位置并将其放置在此字段中为每个学生



public int EngPts {get; set;}

public int MathPts {get; set;}



public student (int engPts,int mathPts)

{

EngPts = engPts;

MathPts = mathPts;

} < br $>
}



这是我的2cents。
void Main()
{
List<student> list = new List<student>();
list.Add(new Student(7,30)); // position of student = 3rd
list.Add(new Student(3,40));// position of student = 6th
list.Add(new Student(5,60));// position of student = 5th
list.Add(new Student(88,70));// position of student = 2nd
list.Add(new Student(2,5));// position of student = 7th
list.Add(new Student(963,90));// position of student = 1st
list.Add(new Student(6,34));// position of student = 4th
list.Add(new Student(963,343));// position of student = 1st
list.Add(new Student(963,343));// position of student = 1st

list.OrderByDescending (c => c.EngPts).ThenByDescending (c => c.MathPts);
//at this point your list should be sorted like so
/*
963 343
963 343
963 90
88 70
7 30
6 34
5 60
3 40
2 5
which seems to match the ranking you are trying to get
*/
}

class Student
{
//public string positionInEnglish;//position to be calculated and placed in this field for each student
//public string positionInMath;//position to be calculated and placed in this field for each student

public int EngPts{get;set;}
public int MathPts{get;set;}

public Student(int engPts,int mathPts)
{
EngPts = engPts;
MathPts = mathPts;
}
}

this is my 2cents.


这篇关于如何计算每个科目的学生的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆