zoukankan      html  css  js  c++  java
  • 0725异常保护和类

               

          一.  异常保护

                 ////try
                ////catch
                //try //保护可能出错的语句
                //{
                //    Console.Write("请输入一个整数:");
                //    int a = int.Parse(Console.ReadLine());
                //    Console.WriteLine("您的输入正确。");
                //}
                //catch //若try的语句有问题,会直接抓住,到这个位置执行
                //{
                //    Console.WriteLine("您的输入有误!");
                //}
                //finally //最后要做什么,写与不写一样
                //{
                //    Console.WriteLine("感谢您的使用,再见!");
                //}
                //Console.ReadLine();

    二.类 

    1.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    是否以**字符串开头
                //Console.WriteLine(s.StartsWith("ab"));
                ////endswith      是否以**字符串结尾
                //Console.WriteLine(s.EndsWith("b"));

                ////contains      是否包含
                //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 mail = Console.ReadLine();
                //if (mail.Contains("@"))
                //{
                //    int a = mail.IndexOf("@");
                //    int b = mail.LastIndexOf("@");
                //    if (a == b)
                //    {
                //        if (!mail.StartsWith("@"))
                //        {
                //            string mail1 = mail.Substring(a);
                //            if (mail1.Contains("."))
                //            {
                //                int c = mail1.IndexOf(".");
                //                if (c != 1)
                //                {
                //                    if (mail.Substring(a - 1, 1) != ".")
                //                    {
                //                        if (!mail.EndsWith("."))
                //                        {
                //                            Console.WriteLine("邮箱格式正确,您输入的邮箱账号是:"+mail);
                //                        }
                //                        else
                //                        {
                //                            Console.WriteLine("您的邮箱格式不正确!");                                   
                //                        }
                //                    }
                //                    else
                //                    {
                //                        Console.WriteLine("您的邮箱格式不正确!");                                   
                //                    }
                //                }
                //                else
                //                {
                //                    Console.WriteLine("您的邮箱格式不正确!");                               
                //                }
                //            }
                //            else
                //            {
                //                Console.WriteLine("您的邮箱格式不正确!");
                //            }
                //        }
                //        else
                //        {
                //            Console.WriteLine("您的邮箱格式不正确!");
                //        }
                //    }
                //    else
                //    {
                //        Console.WriteLine("您的邮箱格式不正确!");
                //    }
                //}
                //else
                //{
                //    Console.WriteLine("您的邮箱格式不正确!");
                //}


                //Console.ReadLine();


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

                //Console.ReadLine();


                //try
                //{
                //    Console.Write("请输入一个时间日期:");
                //    DateTime dt = DateTime.Parse(Console.ReadLine());
                //    Console.WriteLine("您输入的时间日期格式正确!");
                //}
                //catch
                //{
                //    Console.WriteLine("您输入的时间日期有误!");
                //}
                //Console.WriteLine("感谢使用!");
                //Console.ReadLine();


       3.随机数类     Random
                //初始化
                //Random ran = new Random();
                ////int a = ran.Next(101);
                //int b = ran.Next(1,37);

  • 相关阅读:
    python之路----初识面向对象(二)
    python之路----初识面向对象
    python之路----包
    python之路----模块与序列化模块
    python之路----模块调用
    python之路----常用模块二
    Python之路----递归函数
    Python之路----内置函数
    【CSS】整屏大背景
    【PHP】打印输出var_dump+echo+print_r
  • 原文地址:https://www.cnblogs.com/a12110303043/p/5706078.html
Copyright © 2011-2022 走看看