大神,帮忙看一下哪儿错了

来源:6-1 练习题目

qq_万里晴空_1

2017-02-21 13:36

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;
            for(int i = 0;i < score.Length;i++)
            {
                if(max < score[i])
                {
                    max = score[i];
                    index = i;
                }
               
            }
            Console.WriteLine("分数最高的是{0},分数是{1}",names[index],score[index]);
        }
       
    }
   
}

写回答 关注

2回答

  • qq_新城旧梦_03226011
    2017-02-21 14:16:55
    已采纳

    你的index没有赋初始值

    qq_万里晴...

    非常感谢!

    2017-02-21 14:52:05

    共 2 条回复 >

  • qq_万里晴空_1
    2017-02-21 14:26:04

    这样就通过了。

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace projGetMaxScore
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] score = new int[]{89,90,98,56,60,91,93,85};
                string[] names = new string[]{"吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣"};
                int index = 0,max = score[0];
                for(int i = 1;i < score.Length;i++)
                {
                    if(score[i] > max)
                    {
                        max = score[i];
                        index = i;
                    }
                }
                Console.WriteLine("分数最高的是" + names[index] + ",分数是" + max);
            }
           
        }
       
    }

C#开发轻松入门

本门课程是C#语言的入门教程,将带你轻松入门.NET开发

254118 学习 · 1459 问题

查看课程

相似问题