[英]how to calculate the position of students in each subject in a class?
確定更清楚。 我實際上的意思是我有一個數據網格,其中包含不同的列,例如 Id 列、名字和不同主題的列,下面是示例:
when each row from the sample datagrid is selected the all the scores for each subject column is shown in the textbox1. 像這樣:點擊事件前的樣本
並且當使用以下代碼單擊 button1 事件時:
foreach(int mark in txtresult.ToString())
{
if (mark < 40)
{
string[] parts = txtresults.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int[] numbers = parts.Select(p => int.Parse(p)).ToArray();
**int maximum = numbers.Max();**
int position = Array.IndexOf(numbers, maximum);
parts[position] = "1".ToString();
txtresults.Text = string.Join(" ", parts);
}
else if(mark >=40 && <50)
{
m="2th" position etc... within the above block of code
}
每個科目的分數應該根據指定范圍“if(mark <40)”進行排名,但它只是排名Max(); 文本框中的值,而不是文本框中的小值。
所以我的問題是:我如何對數據網格中的每個學生進行評分就像在:列英語中,我希望能夠將最高的學生排名第一,第二高的學生排名第二……等等,以及循環遇到類似分數的地方文本框將有類似的 position 並繼續排名。 直到每個學生的所有分數都被排名; (例如,Eng =56, 78, 89, 76, 89.. 從這個 89 => 1st; 89 =>1st; 78 => 3rd; 76 =>4th position; & 56=> 5th Z4757FE07FD492A8BE0EA6A760D683)
您可以使用 LINQ 對集合的項目進行分組。
以下示例創建三個組:
使用值規則的“低”、“中”、“高”
n < 10
, n >= 10 && n < 100
, n >= 100
:
var numericString = "1 2 12 15 123 757";
var numberGroups = numericString
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(numericString => int.Parse(numericString))
.GroupBy(number => number < 10
? "Low"
: number < 100
? "Mid"
: "High");
// Access the groups
foreach (IGrouping<string, int> numberGroup in numberGroups)
{
string groupName = numberGroup.Key; // E.g. "High"
IEnumerable<int> groupValues = numberGroup; // 123, 757
}
並將相等的值分組:
var numericString = "100 20 12 100 20 20 48 100";
var numberGroups = numericString
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(numericString => int.Parse(numericString))
.GroupBy(number => number);
// Access the groups
foreach (IGrouping<int, int> numberGroup in numberGroups)
{
int groupName = numberGroup.Key; // E.g. 100
List<int> groupValues = numberGroup.ToList(); // 100, 100, 100
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.