1. 程式人生 > >編寫C#程序,計算去除最大值和最小值之後的平均值

編寫C#程序,計算去除最大值和最小值之後的平均值

pub ole eric efault lis ner .get ast c#

有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#程序,計算去除最大值和最小值之後的平均值