练习题目
学习过C#语言的语法,又学习了条件逻辑和循环逻辑,还学习了几种常用的算法,我们是否能够说掌握了编程的本领呢?让我们用一道有些难度的编程练习检验一下吧!
任务
一次考试,各位同学的姓名和分数如下:
请编写程序,输出分数最高的同学的姓名和分数。
----------------------------------------------------------------------------------------------------------------------------------------------------
一:
using System; using System.Collections.Generic; using System.Text; namespace projGetMaxScore { class Program { static void Main(string[] args) { string[] names = { "吴松", "钱东宇", "伏晨", "陈陆", "周蕊", "林日鹏", "何昆", "关欣" }; int[] score = {89,90,98,56,60,91,93,85}; int max = score[0]; //初始化为第一个元素。 int index = 0; //最大值的索引。 for(int i=1;i<score.Length;i++) //i=1是因为第一个元素已经赋值给max了,所以从第二个元素开始比较。 { if(score[i]>max) { max = score[i]; index = i; //记录索引 } } Console.WriteLine("分数最高的是{0},分数是{1}。",names[index],max); } } }
二:
using System; using System.Collections.Generic; using System.Text; namespace projGetMaxScore { class Program { static void Main(string[] args) { string[] names = { "吴松", "钱东宇", "伏晨", "陈陆", "周蕊", "林日鹏", "何昆", "关欣" }; int[] score = {89,90,98,56,60,91,93,85}; int max = score[0]; string name=""; for(int i=1;i<score.Length;i++) { if(score[i]>max) { max=score[i]; name=names[i]; } } Console.WriteLine("分数最高的是{0},分数是{1}。",name,max); } } }
https://www.imooc.com/code/9177