码迷,mamicode.com
首页 > 其他好文 > 详细

类型(Math 、Datetime )

时间:2014-10-20 14:59:09      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   使用   for   strong   sp   数据   2014   

           //double d = double.Parse(Console.ReadLine());

            //d = Math.PI;//圆周率

            //d = Math.Sqrt(d);//开方

            //d = Math.Ceiling(d);//当为整数取整,当小数点后大于0,取上限加1进位取整.

            //d = Math.Floor(d);//舍掉小数点后所有,并取整,不进位

            //d = Math.Round(d);//四舍五入,当整数部分为奇数.5上位,整数部分为偶数.5舍掉

            string s = Console.ReadLine();

            DateTime dt = new DateTime();//初始化默认时间0001年1月1日 12:00:00 am

          //  dt = DateTime.Now;//获取电脑上的本地的时间

           // int i = dt.Year;

            // DayOfWeek d = dt.DayOfWeek;

          //  int i = dt.DayOfYear;//获取日期是当年的第几天,返回int类型值

            dt = DateTime.Parse(s);

           // s = dt.ToString("yyyy是个好年MM月dd");//yyyy是代位符

            System.TimeSpan time = new TimeSpan();//时间间隔类型,最多加到天

          //  dt = dt.Add(time);//参数是TimeSpan类型的值

          //  dt = dt.AddDays(1.2);//加多少天,参数为double

           // dt = dt.AddHours(1.2);//加多少个小时,参数double

            //dt = dt.AddMonths(3);//当前面为31天,超出后面的天数的时候,取最大,不进入下一个月

           // dt = dt.AddYears(2);//当遇到闰年29号,加完不是闰年显示28号,不进入下一个月

 

 /*   //输入年份,判断是不是闰年(测试数据2000,2100,2012,2014)

            int year = 0;

            try

            {                  year = int.Parse(Console.ReadLine());             }

            catch (Exception e)

            {                 Console.Write("输入错误!");             }

        //先判断输入的格式,是不是正确!运行 finally

            finally

            {  if (0 < year && year <= 9999)

                {    if (year % 100 == 0)

                    {    if (year % 400 == 0)

                        {                             Console.Write("是闰年");                         }

                        else

                         {                             Console.Write("不是闰年");                         }

                    }

                    else

                    {

                        if (year % 4 == 0)

                        {                             Console.Write("是闰年");                         }

                        else

                        {                             Console.Write("不是闰年");                         }

                    }

                }

                else

                {                     Console.Write("输入数字超出范围!");                 }

            }

            console.ReadLine();           */

 

/*   //判断年月日是否输入正确(2012/2/29 ,2014/4/31)
             Console.Write("输入日期");
            DateTime dt = new DateTime();
            int i = 0;
            try
            {
                dt = DateTime.Parse(Console.ReadLine());     //验证输入是否有误(使用电脑自己的验证功能)
            }
            catch (Exception e)                              //如果有误,运行catch
            {
                Console.Write("输入错误!");              //输出并且记录,输入发生错误
                i++;
            }
            finally                                               // 运行finally,输出正确!
            {
                if (i==0)
                {
                    Console.Write("输入正确");
                }
            }
             Console.ReadLine();
             */

 

 /*  //输入两个日期,求时间差多少天,具体到天就行

              DateTime dt1 = new DateTime();

              DateTime dt2 = new DateTime();

              int i = 0;               int sum = 0;

              try

              {

                  dt1 = DateTime.Parse(Console.ReadLine());

                  dt2 = DateTime.Parse(Console.ReadLine());

              }

              catch (Exception e)

              {

                  Console.Write("输入错误!");

                  i++;

              }

              finally

              {

                  if (i == 0)

                  {

                      if (dt1 > dt2)

                      {

                          

                          DateTime dt3 = dt2;

                          dt2 = dt1;

                          dt1 = dt3; //等量代换 

                      }

                      for (int k = dt1.Year; k < dt2.Year; k++)      //判断是不是同一年的时间

                      {

                          if (k % 100 == 0)

                          {

                              if (k % 400 == 0)                      //判断…00是不是闰年

                              {

                                  Console.Write(k+"是闰年");

                                  sum += 366;                         //闰年运行一次总天数,加366天

                              }

                              else

                              {

                                  Console.Write(k+"不是闰年");

                                  sum += 365;

                              }

                          }

                          else

                          {

                              if (k % 4 == 0)                        //这里是判断   k  是不是闰年,同上。                                {

                                  Console.Write(k+"是闰年");

                                  sum += 366;

                              }

                              else

                              {

                                  Console.Write(k+"不是闰年");

                                  sum += 365;

                              }

                          }

                      }

                      sum += dt2.DayOfYear - dt1.DayOfYear;                                 //因为把k年全部天数都加在sum,所以减去dt1的天数。加上dt2是因为,k不包含dt2这一年的天数!

                      Console.WriteLine("相差" + sum + "天");

                  }

              }

              Console.ReadLine();            /*

 

 /*    //随机生成4位验证码,并输入比对是否输入正确

               string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";

                 //共63个字符

               Random r = new Random();            //随机生成    r

                  int zi1 = r.Next(63);                //从63个字符中随机产生r,并且赋值给zi1.

               int zi2 = r.Next(63);

               int zi3 = r.Next(63);

               int zi4 = r.Next(63);

               string a = s.Substring(zi1,1);

               string b = s.Substring(zi2, 1);        //   从字符串中截取1个字符,赋值

               string c = s.Substring(zi3, 1);

               string d = s.Substring(zi4, 1);

               string suiji = a+b+c+d;              //设定验证码是abcd组合

               Console.WriteLine(suiji);

                 while (true)

               {

                   string shuru = Console.ReadLine();

                  // shuru = shuru.ToUpper();        //此处表示转化成大写!

                  // suiji = suiji.ToUpper();

                   if (shuru == suiji)              //判断   输入的  和  随机产生的  是否一致!

                   {

                       Console.Write("输入正确");

                       break;                           //输入正确,结束!

                   }

                   else

                   {

                       Console.Write("重新输入");

                   }

               }

              Console.ReadLine();            */

  //控制台读取当前电脑时间,之后输入一个天数,将当前电脑时间加上这个天数,输出是星期几
            DateTime d = DateTime.Now;            //创建  Datetime  d  为系统当前时间
            DateTime a = d.AddDays(1);            //在  d   加上(n)天数 ,注意格式! 
            DayOfWeek  c = a.DayOfWeek;          // 创建 DayOfWeek(星期几)表示a 
            Console.Write(c);
            Console.ReadLine();

 

//判断邮箱格式是否输入正确

            Console.WriteLine("请输入你的邮箱:");

            string semail = Console.ReadLine();

            semail = semail.ToLower();            //全部小写!

            string bidui = "abcdefghijklmnopqrstuvwxyz1234567890_.@";

            int sant = 0;  //表示@的数量           int sdian = 0; //表示.的数量            int zhengque = 0;

            for (int i = 0; i < semail.Length; i++)     //字符串长度,输出int值

            {

                string sc = semail.Substring(i,1);        //截取字符串中第i+1个字符

                bool b = bidui.Contains(sc);                  //创建bool值  b,表示比对   semail   是不是包含sc

                if (b)

                {

                    if (sc == "@")             //注意表达格式!sc==“@”,此处用""

                    {

                        sant++;

                    }

                    if (sc == ".")

                    {

                        sdian++;

                    }                                 //这里判断是不是有   @   .    有几个     

                }

                else

                {

                    Console.WriteLine("您的邮箱有特殊字符,请检查输入!");

                    zhengque++;

                }

            }

            if (sant == 1 && sdian == 1 && zhengque ==0)    //一个@一个.并且没有特殊字符!

            {

                if (semail.Contains("@.") || semail.IndexOf("@")>semail.IndexOf("."))

                                                                        //表示 @ 第一次出现位置数大于 . 第一次出现位置数

                {

                    Console.WriteLine("@和.之间需要有邮箱地址代表字符,并且@必须在.前面");

                }

                else                        // 符合上面要求,进入下一判断条件!

                {

                    if (semail.EndsWith(".com") || semail.EndsWith(".cn"))      //判断结尾是不是.com或.cn

                    {

                        Console.WriteLine("输入正确");

                    }

                    else

                    {

                        Console.WriteLine("邮箱必须以.com或者.cn结尾");

                    }

                }

            }

            else                    //if  、 else   可以一起写,先易后难。否则容易忘了判断的是什么!

            {

                Console.WriteLine("注意邮箱中只能出现一个@和一个.");

            }

             Console.ReadLine();

                /*

 

类型(Math 、Datetime )

标签:io   os   ar   使用   for   strong   sp   数据   2014   

原文地址:http://www.cnblogs.com/huaze/p/4037025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!