DataCalculate.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. static class DataCalculate
  3. {
  4. static void Main()
  5. {
  6. Console.Write("请输入年份:");
  7. int year = int.Parse(Console.ReadLine());
  8. Console.Write("请输入月份:");
  9. int month = int.Parse(Console.ReadLine());
  10. Console.Write("请输入日期:");
  11. int day = int.Parse(Console.ReadLine());
  12. int daysInYear = GetDaysInYear(year, month, day);
  13. Console.WriteLine($"{year}年{month}月{day}日是该年的第{daysInYear}天。");
  14. }
  15. public static int GetDaysInYear(int year, int month, int day)
  16. {
  17. int[] daysInMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  18. if (IsLeapYear(year))
  19. {
  20. daysInMonths[1] = 29;
  21. }
  22. int days = day;
  23. for (int i = 0; i < month - 1; i++)
  24. {
  25. days += daysInMonths[i];
  26. }
  27. return days;
  28. }
  29. public static bool IsLeapYear(int year)
  30. {
  31. return (year % 4 == 0 && year % 100!= 0) || (year % 400 == 0);
  32. }
  33. }