类以及练习——7月25日

一、String 类

////String类
            //string s = "   abCDefgb   ";
            ////int a = s.Length;//获取长度
            //Console.WriteLine(s.Length);

            ////去掉前后空格
            //Console.Write(s.Trim());
            ////只去掉前面的空格
            //Console.Write(s.TrimStart());
            //Console.WriteLine(123);
            ////只去掉后面的空格
            //Console.Write(s.TrimEnd());

            ////将全部小写字母转换为大写
            //Console.WriteLine(s.ToUpper());
            ////将所有大写字母转换为小写
            //Console.WriteLine(s.ToLower());

            ////返回第一次出现该字符或字符串的索引号
            ////注意:索引号是从0开始
            ////返回值为-1.表示没有找到该字符或字符串
            //Console.WriteLine(s.IndexOf("abc"));
            ////返回最后一次出现该字符或字符串的索引号
            //Console.WriteLine(s.LastIndexOf("b"));

            ////substring截取字符串
            ////写一个参数的时候,表示从这个索引号开始截取,一直到最后
            //Console.WriteLine(s.Substring(3));
            ////两个参数表示,从哪个位置开始截取,截取多长
            //Console.WriteLine(s.Substring(4,4));

            ////startswith    是否以**字符串开头,返回bool类型,true或false
            //Console.WriteLine(s.StartsWith("ab"));
            ////endswith      是否以**字符串结尾,返回bool类型,true或false
            //Console.WriteLine(s.EndsWith("b"));

            ////contains      是否包含,返回bool类型,true或false
            //Console.WriteLine(s.Contains("CD"));

            ////replace       替换
            //Console.WriteLine(s.Replace("b","BB"));

            //Console.WriteLine(s);
            //Console.ReadLine();

练习一:输入身份证号,截取出生日期

//请输入您的身份证号,为您截取出来您的生日
            //370321199003053330
            //Console.Write("请输入您的身份证号:");
            //string cid = Console.ReadLine();
            //string year = cid.Substring(6,4);
            //string month = cid.Substring(10,2);
            //string day = cid.Substring(12,2);
            //Console.WriteLine("您的出生日期为:{0}年{1}月{2}日。",year,month,day);
            //Console.ReadLine();

练习二:输入邮箱判断格式是否正确

////练习:输入您的身份证号,为您截取出来您的生日
            ////输入一个邮箱,判断邮箱格式是否正确
            ////1.有且只有一个@
            ////2.不能以@开头
            ////3.@之后至少有一个 .
            ////4.@和.不能靠在一起,前后都不可以
            ////5.不能以.结尾

            Console.Write("请输入一个邮箱:");
            string yx = Console.ReadLine();
            int a = yx.IndexOf("@");
            int b = yx.LastIndexOf("@");
            if (a == b)
            {
                bool c = yx.StartsWith("@");
                if (c == false)
                {
                    string d = yx.Substring(a);
                    bool e = yx.Contains(".");
                    if (e == true)
                    {
                        int f = d.IndexOf(".");
                        if (f != 1)
                        {
                            string g = yx.Substring(a-1,1);
                            if (g != ".")
                            {
                                bool h=yx.EndsWith(".");
                                if (h == false)
                                {
                                    Console.WriteLine("您输入的邮箱格式正确!您输入的账号是:"+yx);
                                }
                                else
                                {
                                    Console.WriteLine("您的输入有误!");
                                }
                            }
                            else
                            {
                                Console.WriteLine("您的输入有误!");
                            }
                        }
                        else
                        {
                            Console.WriteLine("您的输入有误!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("您的输入有误!");
                    }
                }
                else
                {
                    Console.WriteLine("您的输入有误!");
                }
            }
            else
            {
                Console.WriteLine("您的输入有误!");
            }

            Console.ReadLine();

二、Math 类

////Math类    数学类
            ////ceiling    天花板   取上线
            //Console.WriteLine(Math.Ceiling(4.4));
            ////floor     地板      取下线
            //Console.WriteLine(Math.Floor(4.4));
            ////sqrt      开平方根
            //Console.WriteLine(Math.Sqrt(4));
            ////pi        π    3.141592
            //Console.WriteLine(Math.PI);
            ////round     四舍五入
            ////奇数.5的时候取得是上线
            ////偶数.5的时候取得是下线
            //Console.WriteLine(Math.Round(4.5));
            //Console.ReadLine();

三、DateTime  时间日期类型

//DateTime      时间日期类型
            //使用之前应该进行初始化
            //DateTime dt = new DateTime();
            //获取当前时间话可以不用初始化
            //DateTime dt = DateTime.Now;
            //Console.WriteLine(dt);

            //Console.WriteLine(dt.Month);
            //获取年        dt.Year
            //获取月        dt.Month
            //获取日        dt.Day
            //获取小时        dt.Hour
            //获取分        dt.Minute
            //获取秒        dt.Second

练习一:获取星期几

//获取星期几
            //DayOfWeek d = dt.DayOfWeek;
            ////Console.WriteLine(d);
            //string dow =d.ToString();
            //switch(dow)
            //{
            //    case "Monday":
            //        Console.WriteLine("星期一");
            //        break;
            //    case "Tuesday":
            //        Console.WriteLine("星期二");
            //        break;
            //}

练习二:输入两个日期,计算相差几天

//Add()     增加或减少
            //TimeSpan span = new TimeSpan(3, 3, 3, 3);
            //Console.WriteLine(dt.Add(span));

            ////增加多少天
            //Console.WriteLine( dt.AddDays(-7.5));


            //输入两个时间日期,计算出相差多少天(TotalDays)
            //Console.Write("请输入第一个时间日期(****/**/** **:**:**):");
            //DateTime dt1 = DateTime.Parse(Console.ReadLine());
            //Console.Write("请输入第二个时间日期(****/**/** **:**:**):");
            //DateTime dt2 = DateTime.Parse(Console.ReadLine());

            //Console.WriteLine((dt2-dt1).TotalDays);

练习三:QQ情侣空间计时

//QQ情侣
            //Console.Write("请输入你们恋爱开始的时间日期:");
            //DateTime dt1 = DateTime.Parse(Console.ReadLine());
            //DateTime dt2 = DateTime.Now;
            //Console.WriteLine("已经恋爱了{0}天。",Math.Ceiling( (dt2-dt1).TotalDays));

            //Console.ReadLine();

练习四:高考倒计时

四、Random 随机数类

////随机数类   Random
            ////初始化
            //Random ran = new Random();
            //int a = ran.Next(101);//表示的是随机数a是100以内的数,如果写100,那只能取到99.所以要写101
            ////方法2:
            //int b = ran.Next(1,37);//表示随机数b在1~37这个范围内,
原文地址:https://www.cnblogs.com/juyangchao12/p/5709226.html