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;
}
}
}
结构体demo
2024-03-28 16:54:01 41 阅读