为了账号安全,请及时绑定邮箱和手机立即绑定

各位大佬请帮我简化下我的代码,感觉有些复杂

 static void Main(string[] args)
        {
            //声明 两组数组 ;
            int[] score =new int[]{89,90,98,86,60,91,93,85};
            string[] name = new string []{"吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣"};
            //声明 分数中的 最大值;
            int max = score[0];
            //判断出了最大的分数;
            for (int i =0;i<score.Length;i++)
            {
                max =max>score[i]?max:score[i];

            }
            //现在要判断出分数的拥有者,即分数在数组中的具体位置
             for(int j = 0;j<score.Length;j++)
             {
                 if(max == score[j])
                 {
                    Console.Write("分数最高的是"+ name[j]+ ",分数是"+max);
                   
                 }
             }
   
        }


正在回答

3 回答

 static void Main(string[] args)
        {
            //声明两组数组 ;
            int[] score =new int[]{89,90,98,86,60,91,93,85};
            string[] name = new string []{"吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣"};
            //声明分数中的最大值和得分者;
            int max = 0;

            string topName = null;
            //判断出了最大的分数;
            for (int i =0;i<score.Length;i++)
            {
                if (score[i]>max)

                max =score[i];

                topName = name[i];

            }
                    Console.Write("分数最高的是{0},分数是{1}", topName, max);//输入结果
                   
    }


0 回复 有任何疑惑可以回复我~
#1

见微知著

if下面不加大括号的话,名字会输出错误
2018-09-27 回复 有任何疑惑可以回复我~
#2

微风拂过v

为什么我for (int i =0;i<score.Length;i++)换成foreach(int i in score),程序就不好使了呢?
2018-10-15 回复 有任何疑惑可以回复我~

static void Main(string[] args)
        {
            //声明两组数组 ;
            int[] score =new int[]{89,90,98,86,60,91,93,85};
            string[] name = new string []{"吴松","钱东宇","伏晨","陈陆","周蕊","林日鹏","何昆","关欣"};
            //声明分数中的最大值和得分者;
            int max = 0;

            string topName = null;
            for (int i =0;i<score.Length;i++)   //判断出了最大的分数;            {
                if (score[i]>max)

              {

                max =score[i];

                topName = name[i];

               }

            }
                    Console.Write("分数最高的是{0},分数是{1}", topName, max);//输入结果
                    
    }


0 回复 有任何疑惑可以回复我~

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

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

            int max = 0;

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

            {

                max = num[max] > num[i] ? max : i;

            }

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


0 回复 有任何疑惑可以回复我~
#1

微风拂过v

请问你写的程序里 max = num[max] > num[i] ? max : i; 如果把?:换用if...else表达是怎样的?
2018-10-15 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

各位大佬请帮我简化下我的代码,感觉有些复杂

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号