首页 > 解决方案 > 制作一个studentscore程序,将数组中的分数分配给成绩

问题描述

嗨,我是 C# 编程的新手,我需要帮助来制作一个程序来检查得分的学生人数

A(80以上) B(70以上) C(60以上) D(50以上) F(50以下)

但我有一个给定标记的分配数组,所以我不能更改标记。

int[] marks = new []{ 90, 40, 60, 80, 100, 20, 40, 60, 80, 90 };

然后,我必须输出达到以下成绩的学生数量:

A:获得“A”的学生数量。B:获得“B”的学生数量。如果有的话,直到“F”级。

如果我的问题没有很好地说明,请问我任何问题。我只学习了 C# 的基础知识,例如 if-else、while 循环、for 循环,但还没有学到很多东西。请注意我的英语,因为它不是我最擅长的语言。

标签: c#

解决方案


整个程序大部分是初学者友好的代码。

// Initial scores
int[] scores = new int[] { 90, 40, 60, 80, 100, 20, 40, 60, 80, 90 };

// Marks keep track of how many times each mark was given
Dictionary<string, int> marks = new Dictionary<string, int>();
// This is a counter, so init every mark count with 0
marks.Add("A", 0);
marks.Add("B", 0);
marks.Add("C", 0);
marks.Add("D", 0);
marks.Add("F", 0);

// Go through all the scores and give marks
foreach(int score in scores)
{
    if(score >= 80)
    {
        marks["A"]++;
    }
    else if(score >= 70)
    {
        marks["B"]++;
    }
    else if(score >= 60)
    {
        marks["C"]++;
    }
    else if(score >= 50)
    {
        marks["D"]++;
    }
    else
    {
        marks["F"]++;
    }
}

// Finally output every mark
Console.WriteLine("A: " + marks["A"]);
Console.WriteLine("B: " + marks["B"]);
Console.WriteLine("C: " + marks["C"]);
Console.WriteLine("D: " + marks["D"]);
Console.WriteLine("F: " + marks["F"]);

如果您在理解某些概念时遇到困难,请查看一些教程:

词典:https ://www.youtube.com/watch?v=fMjt6ywaSow

Foreach:https ://www.youtube.com/watch?v=ymgp77c2aR0


推荐阅读