c# - 制作一个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 循环,但还没有学到很多东西。请注意我的英语,因为它不是我最擅长的语言。
解决方案
整个程序大部分是初学者友好的代码。
// 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"]);
如果您在理解某些概念时遇到困难,请查看一些教程:
推荐阅读
- java - Java Web 应用程序与 MySQL 的本地连接问题,但即使在远程服务器上也是如此
- c# - 随着时间的推移来回改变光强度
- javascript - 修复 IE11 中 DagreD3 边缘箭头填充
- pdf - EvoPdf 和 FIPS
- design-patterns - 访问者模式的常见用例是什么?
- vba - 通过 Excel VBA 在 Outlook 上进行高级搜索
- python - 具有 NaN 值的 Python pandas 拆分列
- vba - 将文本转换为数字的宏会产生不一致的结果
- python-3.x - 我如何在我的 websocket 龙卷风中获得动态网址?
- python-3.x - 捕获python setuptools的命令?