有没有更简单的代码

来源:6-1 练习题目

慕粉2003103133

2018-07-17 09:54

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

namespace projGetMaxScore
{
    class Program
    {
        static void Main(string[] args)
        {
              object [,] names = new object[8, 2]{
                {"吴松",89},{"钱东宇",90},{"伏晨",98},{"陈陆",56},{"周蕊",60},{"林日鹏",91},{"何昆",93},{"关欣",85}
            };
            object temp = 0;
            object maxscore = 0;
            object name = null;
            for (int i = 0; i < names.GetLongLength(0); i++)
            {
                temp = names[i, 1];
                if ((int)maxscore < (int)temp)
                {
                    maxscore = temp;
                    name = (string)names[i, 0];
                }
                else
                {
                    continue;
                }
            }
            Console.Write("分数最高的是" + name + ",分数是" + maxscore+",");
        }
    }
}

写回答 关注

7回答

  • 唐墨痕
    2018-07-19 08:46:13
    已采纳

    string [] name = new string [] { "吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣" };

                int [] branch = new int [] { 89,90,98,56,60,91,93,85};

                int zuigao = 0;

                int index = 0;

                for(int i=0;i<branch.Length;i++){

                    if(zuigao>branch[i] == false){

                        zuigao = branch[i];

                        index = i;

                    }

                }

                Console.Write("分数最高的是{0},分数是{1}",name[index],branch[index]);


  • 慕婉清9144518
    2018-09-03 17:00:35

    string [] name = new string [] { "吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣" };

                int [] score = new int [] { 89,90,98,56,60,91,93,85};

                int high = 0;

                int index = 0;

                for(int i=0;i<score.Length;i++)

                {

                    if(high<score[i])

                    {

                        high = score[i];

                        index = i;

                    }

                }

                Console.WriteLine("分数最高的是{0},分数是{1}",name[index],score[index]);


  • 星辰的泪
    2018-07-31 14:10:02

    string [,] test = new string[8,2]{{"吴松","89"},{"钱东宇","90"},{"伏晨","98"},{"陈陆","56"},{"周蕊","60"},{"林日鹏","91"},{"何昆","93"},{"关欣","85"}};

                int max = 0;

                int index=0;

                for (int i=0;i<test.GetLongLength(0);i++)

                {

                    if (max<int.Parse(test[i,1]))

                    {

                        max=int.Parse(test[i,1]);

                        index=i;

                    }

                }

                Console.Write("分数最高的是{0},分数是{1}",test[index,0],max);


  • 慕粉2003103133
    2018-07-31 09:43:38

    我把object的声明改了。使用了两种做法。

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

    namespace projGetMaxScore
    {
        class Program
        {
            static void Main(string[] args)
            {
                //第一种方法
                  string [,] names = new string[8, 2]{
                    {"吴松","89"},{"钱东宇","90"},{"伏晨","98"},{"陈陆","56"},{"周蕊","60"},{"林日鹏","91"},{"何昆","93"},{"关欣","85"}
                };
                int temp = 0;
                int maxscore = 0;
                string name = null;
                for (int i = 0; i < names.GetLongLength(0); i++)
                {
                    temp = int.Parse(names[i, 1]);
                    if (maxscore < temp)
                    {
                        maxscore = temp;
                        name = names[i, 0];
                    }
                    else
                    {
                        continue;
                    }
                }
                Console.Write("分数最高的是" + name + ",分数是" + maxscore+",");
               
                //第二种方法索引法。
                 string[,] info = new string[8, 2] { { "吴松", "89" }, { "钱东宇", "90" }, { "伏晨", "98" }, { "陈陆", "56" }, { "周蕊", "60" }, { "林日鹏", "9" }, { "何昆", "93" }, { "关欣", "85" } };
                  int temp2 = 0;
                  for (int i = 0; i < info.GetLongLength(0); i++)
                  {
                      if (int.Parse(info[i, 1]) > int.Parse(info[temp2, 1]))
                      {
                          temp2 = i;
                      }
                  }
                  Console.Write("分数最高的是" + info[temp2, 0] + ",分数是" + info[temp2, 1]);
            }
        }
    }

  • qq_初五_3
    2018-07-25 17:47:07

    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 maxName=names[0];

                for(int i=0;i<score.Length;i++)

                {

                    if(score[i]>MAX)

                    {

                    MAX=score[i];

                    maxName=names[i];

                    }

                }

                Console.Write("分数最高的是{0},分数是{1}",maxName,MAX);

            }

        }

    }


  • 慕仰4168971
    2018-07-20 09:30:06

    请问为什么要用object?

    慕粉2003... 回复qq_初五_...

    object已经改掉了

    2018-07-31 09:44:47

    共 2 条回复 >

  • 慕粉2003103133
    2018-07-17 09:58:46

    好好好

C#开发轻松入门

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

254118 学习 · 1459 问题

查看课程

相似问题