结构体demo

该文章展示了如何在C#中使用结构体实现日期处理(获取星期和一年中的天数)以及计算一组学生成绩的总分和平均分。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace structdemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //根据输入的年月日,提示这个日期是周几,是一年中第多少天.
            Console.WriteLine("inpyt year");
            string strYear = Console.ReadLine();
            Console.WriteLine("inpyt month");
            string strmonth = Console.ReadLine();
            Console.WriteLine("inpyt date");
            string strday = Console.ReadLine();
            Mydate mydate = new Mydate(strYear, strmonth, strday);   
            Console.WriteLine($"{strYear}-{strmonth}-{strday}是{mydate.getdayofweek()}");
            Console.WriteLine($"{strYear}-{strmonth}-{strday}是一年中第{mydate.getdayofyear()}天.");
            Console.ReadKey();

            //创建N个结构体对象,计算总成绩和平均成绩.
            List<StudentsGrade> studentsscores = new List<StudentsGrade>
            {
                new StudentsGrade("Li",90),
                new StudentsGrade("Zhang",68.5),
                new StudentsGrade("Li",78),
                new StudentsGrade("Li",86),
                new StudentsGrade("Li",99),
                new StudentsGrade("Li",48),
                new StudentsGrade("Li",77.5),
                new StudentsGrade("Li",69.5),
            };
            List<double> scores = studentsscores.Select(students => students.Fenshu).ToList();
            List<string> names = studentsscores.Select(s => s.Name).ToList();
            double sum = 0;
            foreach (var score in scores)
            {
                sum += score;
            }
            double avg = Math.Round(sum / scores.Count, 2);
            Console.WriteLine($"total scores:{sum},avg score:{avg}");
            Console.ReadKey();
        }
    }

    struct StudentsGrade
    {
        private string _name;
        private double _fenshu;
        public StudentsGrade(string name,double fenshu)
        {
            this._name = name;
            this._fenshu = fenshu;
        }

        public string Name { get => _name; set => _name = value; }
        public double Fenshu { get => _fenshu; set => _fenshu = value; }
    }

    struct Mydate
    {
        string _year;
        string _month;
        string _day;
        public Mydate(string year,string month,string day)
        {
            this._day = day;
            this._month = month;
            this._year = year;
        }

        public string getdayofweek()
        {
            DateTime then = new DateTime(int.Parse(_year), int.Parse(_month),int.Parse( _day));

            return then.DayOfWeek.ToString();
        }

        public int getdayofyear()
        {
            return new DateTime(int.Parse(_year), int.Parse(_month), int.Parse(_day)).DayOfYear;
        }
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值