练习题目
学习过 C#语言的语法,又学习了条件逻辑和循环逻辑,还学习了几种常用的算法,我们是否能够说掌握了编程的本领呢?让我们用一道有些难度的编程练习检验一下吧!
一次考试,各位同学的姓名和分数如下:
| 姓名 | 分数 |
|---|---|
| 吴松 | 89 |
| 钱东宇 | 90 |
| 伏晨 | 98 |
| 陈陆 | 56 |
| 周蕊 | 60 |
| 林日鹏 | 91 |
| 何昆 | 93 |
| 关欣 | 85 |
请编写程序,输出分数最高的同学的姓名和分数。运行效果如下:
分数最高的是伏晨,分数是98
代码
csharp
using System;
using System.Collections.Generic;
using System.Text;
namespace projGetMaxScore
{
class Program
{
static void Main(string[] args)
{
string[] names = new string[] {"吴松", "钱东宇", "伏晨", "陈陆", "周蕊", "林日鹏", "何昆", "关欣"};
int[] scores = new int[] {89,90,98,56,60,91,93,85};
int maxScore = scores[0];
string maxScoreName = names[0];
for (int i = 1; i < scores.Length; ++i) {
if (scores[i] > maxScore) {
maxScore = scores[i];
maxScoreName = names[i];
}
}
Console.WriteLine($"分数最高的是{maxScoreName},分数是{maxScore}");
}
}
}