黑马程序员技术交流社区

标题: 写了一个用类实现输入年月日,判断这一天是这一年第几天 [打印本页]

作者: 熊丽    时间: 2013-8-10 02:10
标题: 写了一个用类实现输入年月日,判断这一天是这一年第几天
这几天在论坛看到很多马友,在讨论类的问题,写了一个用类实现输入年月日,判断这一天是这一年第几天的程序。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace exam2
  6. {
  7.     class Date
  8.     {
  9.         protected string year;
  10.         protected string month;
  11.         protected string day;

  12.         protected string  InitYear(string yy) //初始化年
  13.         {
  14.             this.year = yy;
  15.             return this.year;
  16.         }
  17.         protected string InitMonth(string yy) //初始化月
  18.         {
  19.             this.month = yy;
  20.             return this.month;
  21.         }
  22.         protected string InitDay(string yy) //初始化年
  23.         {
  24.             this.day = yy;
  25.             return this.day;
  26.         }
  27.         protected bool IsRuiNian(string yy)  //判断是不是闰年
  28.         {
  29.             int YY=int.Parse(yy);
  30.             if (YY % 400 == 0 || (YY % 4 == 0 && YY % 100 == 0))
  31.                 return true;
  32.             else
  33.                return false;
  34.         }

  35.         protected int[,] GetMonth(string year) //获得月份的天数
  36.         {
  37.             
  38.             int[,] Mounth = { {1,31},{2,28},{3,31},{4,30},{5,31},{6,30},{7,31},{8,31},{9,30},{10,31},{11,30},{12,31}};
  39.             if (IsRuiNian(year))
  40.                 Mounth[1,1] = 29;
  41.             return Mounth;
  42.         }

  43.         public int GetDays(string year, string month, string day)//计算总天数
  44.         {
  45.             int SumDay = int.Parse(day);//总天数
  46.             int [,]Mounth = this.GetMonth(year);
  47.             int mm = int.Parse(month);
  48.             for (int i = 1; i < mm;i++ )//计算从1月到输入的月份的天数
  49.             {
  50.                 SumDay += Mounth[i, 1];
  51.             }
  52.             
  53.             return SumDay;

  54.         }
  55.     }
  56.     class Program
  57.     {

  58.         //输入某年某月某日,判断这一天是这一年的第几天?
  59.         static void Main(string[] args)
  60.         {
  61.             Date date=new Date();
  62.             Console.Write("年:");
  63.             string year = Console.ReadLine();
  64.             Console.Write("月:");
  65.             string month = Console.ReadLine();
  66.             Console.Write("日:");
  67.             string day = Console.ReadLine();
  68.             Console.WriteLine(string.Format("{0}年{1}月{2}日是第{3}天:", year, month, day, date.GetDays(year, month, day)));//用Format格式化输出
  69.             Console.ReadKey();
  70.             
  71.         }
  72.     }
  73. }
复制代码

作者: 许庭洲    时间: 2013-8-10 06:06
值得学习ing!
作者: 彭家贰小姐    时间: 2013-8-10 09:16
{:soso_e179:}
作者: 匿名    时间: 2013-8-10 16:17
  if (YY % 400 == 0 || (YY % 4 == 0 && YY % 100 == 0))                 return true;             else                return false;
作者: sxdxgzr@126.com    时间: 2013-8-10 16:18
瑞年判定:能被4整除但不能被100整除 或 能被400整除 ,貌似代码里有笔误哦。
作者: 熊丽    时间: 2013-8-10 16:42
sxdxgzr@126.com 发表于 2013-8-10 16:18
瑞年判定:能被4整除但不能被100整除 或 能被400整除 ,貌似代码里有笔误哦。 ...

是哦,O(∩_∩)O谢谢指正哦
作者: 心动行动    时间: 2013-8-10 22:06
思路还是很清晰,但是我不会告诉你DateTime这个类当中就有一个方法直接就可以算出一天是今天的第几天
作者: 熊丽    时间: 2013-8-10 22:51
心动行动 发表于 2013-8-10 22:06
思路还是很清晰,但是我不会告诉你DateTime这个类当中就有一个方法直接就可以算出一天是今天的第几天 ...

呵呵,这我真不知道,O(∩_∩)O谢谢咯
作者: 熊丽    时间: 2013-8-10 22:54
心动行动 发表于 2013-8-10 22:06
思路还是很清晰,但是我不会告诉你DateTime这个类当中就有一个方法直接就可以算出一天是今天的第几天 ...

这是看到第八期上活动的题目,所以想写来玩玩,练练手
作者: 心动行动    时间: 2013-8-10 23:04
我也是基础测试题中有这道题,然后我也想着像你这样做,最后看了下DateTime发现简单太多
作者: 熊丽    时间: 2013-8-11 21:36
心动行动 发表于 2013-8-10 23:04
我也是基础测试题中有这道题,然后我也想着像你这样做,最后看了下DateTime发现简单太多 ...

确实简单了很多哟
作者: jdedffjef83j    时间: 2014-12-5 20:33
练习总是好的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2