码迷,mamicode.com
首页 > Windows程序 > 详细

编写C#程序,计算去除最大值和最小值之后的平均值

时间:2017-07-12 16:20:35      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:ram   for   code   summary   linq   eric   eid   exce   turn   

有10位评委对跳水运动员做评分,编写C#程序,计算去除最大得分和最小得分之后的平均得分 作为运动员的跳水成绩。

interface IMark

using System.Collections.Generic;

namespace ConsoleApp1
{
    /// <summary>
    /// 评分接口
    /// </summary>
    interface IMark
    {

        Mark GetMaxScore(List<Mark> list_Marks);

        Mark GetMinScore(List<Mark> list_Marks);

        /// <summary>
        /// 获取所有评分
        /// </summary>
        /// <returns></returns>
        List<Mark> GetAllMarks(int athleteId);

        /// <summary>
        /// 获取除最大得分和最小得分之后的平均得分
        /// </summary>
        /// <returns></returns>
        float GetAvgScoreExceptFirstAndLast();
    }
}

class Mark

namespace ConsoleApp1
{
    /// <summary>
    /// 评分实体
    /// </summary>
    public class Mark
    {
        public int Id;
        public int JuryId;
        public int AthleteId;
        public float Score;
    }
}

class MarkService

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    /// <summary>
    /// 实现评分接口
    /// </summary>
    public class MarkService : IMark
    {  
        public MarkService()
        { 
        }

        /// <summary>
        /// 获取所有评分
        /// </summary>
        /// <param name="athleteId"></param>
        /// <returns></returns>
        public List<Mark> GetAllMarks(int athleteId)
        {
            List<Mark> list = new List<Mark>();
            for (int i = 1; i < 11; i++) {
                list.Add(new Mark()
                {
                    Id = i,
                    JuryId = i,
                    AthleteId = 1,
                    Score = i,                    
                });
            }
            return list.Where(x=>x.AthleteId ==athleteId).ToList();
        } 

        /// <summary>
        /// 获取除最小分数、最大分数之外的平均得分
        /// </summary>
        /// <returns></returns>
        public float GetAvgScoreExceptFirstAndLast()
        {
            List<Mark> List_Marks = this.GetAllMarks(1);
            return List_Marks.Where(
                x => x.Id != this.GetMaxScore(List_Marks).Id &&
                    x.Id != this.GetMinScore(List_Marks).Id).Average(y=>y.Score); 
        }

        /// <summary>
        /// 获取最大得分
        /// </summary>
        /// <param name="list_Marks"></param>
        /// <returns></returns>
        public Mark GetMaxScore(List<Mark> list_Marks)
        {
            float maxScore = list_Marks.Max<Mark>(x => x.Score);
            return list_Marks.Where(x => x.Score == maxScore).FirstOrDefault();
        }

        /// <summary>
        /// 获取最小得分
        /// </summary>
        /// <param name="list_Marks"></param>
        /// <returns></returns>
        public Mark GetMinScore(List<Mark> list_Marks)
        {
            float minScore = list_Marks.Min<Mark>(x => x.Score);
            return list_Marks.Where(x => x.Score == minScore).FirstOrDefault(); 
        }

        
    }
}

 

class Program

using System;
using System.Text; 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IMark t = new Test();
            Console.WriteLine(t.GetAvgScoreExceptFirstAndLast());

            Console.Read();

        }
    }
}

 

编写C#程序,计算去除最大值和最小值之后的平均值

标签:ram   for   code   summary   linq   eric   eid   exce   turn   

原文地址:http://www.cnblogs.com/tcli/p/7155840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!