zoukankan      html  css  js  c++  java
  • 用DateTime类做判断闰年的题目

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 闰年练习
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1、输入年月日,看输入的日期是否正确
                //2、输出是今年是否是闰年(29天),输出日期
                //   闰年的判断普通年能被4整除且不能被100整除的为闰年
                //   世纪年能被400整除的是闰年
                //3、输出是今年的第几天
    
                //提示用户输入日期
                DateTime dt = new DateTime();
                int a = dt.Year;//定义年的变量是a
                int b = dt.Month;//定义月的变量是b
                int c = dt.Day;//定义日的变量是c
                Console.Write("请输入日期:");
                try//异常语句处理,判断用户输入是否正确,如果正确执行下面内容
                {
                    string s = Console.ReadLine();
                    dt = DateTime.Parse(s);
                    s = dt.ToString("yyyy年MM月dd日");
                    if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)
                    {
                        Console.WriteLine("今年是闰年!");
                    }
                    else
                    {
                        Console.WriteLine("今年不是闰年!");
                    }
                    string x = dt.DayOfWeek.ToString();//将英文星期几转换为中文
                    switch(x)
                    {
                        case "Monday":     x = "星期一"; break;
                        case "Tuesday":    x = "星期二"; break;
                        case "Wednesday":  x = "星期三"; break;
                        case "Thursday ":  x = "星期四"; break;
                        case "Friday":     x = "星期五"; break;
                        case "Saturday":   x = "星期六"; break;
                        case "Sunday":     x = "星期七"; break;
                    }
                    Console.WriteLine("您输入的日期是" + a + "年的第" + (dt.DayOfYear) + "天!" + "	" + x);
                }
                catch//用户输入错误
                {
                    Console.WriteLine("对不起,您输入的日期不正确!");
    
                }
    
    
    
                Console.ReadLine();
            }
    
        }
    }
  • 相关阅读:
    UVa中国麻将(Chinese Mahjong,Uva 11210)
    Nginx-upstream模块
    Nginx-配置文件
    Nginx 负载均衡和反向代理实践
    Nginx-1
    linux下发送报警邮件(mailx)
    dns服务器搭建
    linux 时间相关
    Centos7调整swap分区
    rm 删除命令
  • 原文地址:https://www.cnblogs.com/fengsantianya/p/5493687.html
Copyright © 2011-2022 走看看