c#动脑小题目

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {
           string[] name = new string[8] { "吴松", "钱东宇", "伏晨", "陈陆", "周蕊", "林日鹏", "何坤", "关欣" };
           int[] score = new int[8]{89,90,98,56,60,91,93,85};


           //先判断这个数组必须大于0,确保这个数组有长度
           if (score.Length > 0) {
               int index = 0;//然后初始化下标index
               int maxScore = score[0];//这句代码把数字第一位当做是最高分


               for (int i = 0; i < score.Length; i++) {//根据数组的长度进行循环


                   if (score[i] > maxScore) {//然后用这个假设的最高分和数组每个数进行比大小,如果假设的最高分遇到更高的


                       maxScore = score[i];//就把这个更高的分数赋值给maxScore,让maxScore成为真正的最高分


                       index = i;//此时score[i]中的这个i也就是最高分的下标啦
                   }
               }
               Console.Write("分数最高的是"+name[index]+",分数是"+ maxScore);
           }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/cameronanderson/article/details/79463771