码迷,mamicode.com
首页 > Windows程序 > 详细

c#:类 相关练习;

时间:2015-11-07 00:59:50      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

1. 

技术分享

2. int i = a.Length;//获取字符串的长度
   a = a.ToLower();//将字符串中的大写英文字符转化为小写
   a = a.ToUpper();//将字符串中的小写英文字符转化为大写
   a = a.Substring(4,4);
  //索引是从0开始,长度是从1开始
  //substring第一个数是从哪个索引开始截取,第二个是截取多长
  a = a.Trim();//去掉字符串前后的空格
  a = a.TrimStart();//去掉前面的空格
   a = a.TrimEnd();//去掉后面的空格
   Reaplace("字","子");//查找替换功能

3. a = Math.Sqrt(a);//平方根
    Math.Round();//四舍五入
    Math.PI//圆周率
    Math.Ceiling(); //上限
    Math.Floor();//下限

4. Datetime
    DateTime dt = new DateTime();
    dt = DateTime.Now;
    int a = dt.Year;
    int b = dt.Month;
    int c = dt.Day;
    int d = dt.Hour;
    int e = dt.Minute;
    int f = dt.Second;
    int g = dt.DayOfYear;
    DayOfWeek h = dt.DayOfWeek;
    Console.WriteLine(dt);
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
    Console.WriteLine(e);
    Console.WriteLine(f);
    Console.WriteLine(g);
    Console.WriteLine(h);
    Console.ReadLine();

5. int a = 5;
    int b = 7;
    Console.WriteLine("a=(0),b=(1)", a, b);
    //占位符,索引从0开始
    Console.ReadLine();

6. string s = Console.ReadLine();
    //****/**/** **.**.**.**
    DateTime dt = new DateTime();
    dt = DateTime.Parse(s);
    Console.WriteLine(dt);
    Console.ReadLine();

7. yyyy - 年 - year
    MM - 月 -month
    dd -日 -day
    hh -时 -hour
    mm -分 -minute
    ss -秒 -second

    string s = Console.ReadLine();
    DateTime dt = new DateTime();
    dt = DateTime.Parse(s);
    s = dt.ToString("yyyy年MM月dd日hh时mm分ss秒");
    Console.WriteLine(s);
    Console.ReadLine();

8. string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012346789";
    bool b = ss.StartsWith("abc");//是否以这样的字符开头,返回True或者false
    bool b = ss.EndsWith("7889");//是否是以这样的字符作为结尾
    bool b = ss.Contains("ABC");//是否包含这样的字符

    int i = ss.IndexOf("i");//第一个这样的字符所在的索引号
    int j = ss.LastIndexOf("j");//最后一个这样的字符出现的索引号

    double d = 3.14;
    string s = d.ToString("#.#.00");//小数点之前的每三位画一个逗号
    string s = d.ToString("#.00");//小数点之后没有的显示00,有的显示本身
    string s = d.ToString("#.##");//小数点后没有的显示整数部分,有的显示本身

    double b = double.Parse(Console.ReadLine());
    b = Math.Round(b);//奇数的.5取得是上限,偶数的.5取得是下限

9. DateTime dt = DateTime.Now;
    DateTime ct = new DateTime();
    ct = DateTime.Parse("2008/10/10 10:00:00");
    double i = (dt - ct).TotalDays;


    Console.WriteLine(i);
    Console.WriteLine();
    Console.ReadLine();

10. 随机生成一个四位的验证码
     string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012346789";
     //生成四个随机数
     Random ra = new Random();
     int a = ra.Next(62);//括号内62的意思是,随机生成62以下的正整数
     int b = ra.Next(62);
     int c = ra.Next(62);
     int d = ra.Next(62);
     //根据四个随机数截取都是长度为1的字符
     string aa = ss.Substring(a,1);
     string bb = ss.Substring(b, 1);
     string cc = ss.Substring(c, 1);
     string dd = ss.Substring(d, 1);
     //截取出来的字符拼接成一个完整的字符串
     string suiji = aa + bb + cc + dd;
     //打印出来,比着输入验证码
     Console.WriteLine("验证码"+suiji);
     Console.Write("请输入验证码:");
     string shu = Console.ReadLine();

     suiji = suiji.ToUpper();//统一大小写
     shu = shu.ToUpper();//统一大小写
     if (suiji == shu)
    {
     Console.WriteLine("输入正确");
     }
     else
     Console.WriteLine("输入错误");
     Console.ReadLine();

11. 输入天数n,打印出来n天后是星期几

      DateTime dt = DateTime.Now;
      Console.Write("请输入天数n:");
      int i = int.Parse(Console.ReadLine());
      dt = dt.AddDays(i);
      DayOfWeek day = dt.DayOfWeek;
      string s = day.ToString();
      if(s=="Monday")
      {
      Console.WriteLine("星期一");
      }
      else if (s == "Tuesday")
      { 
      Console.WriteLine("星期二");
      }
      else if (s == "Wednesday")
      {
      Console.WriteLine("星期三");
      }
      else if (s == "Thursday")
     {
      Console.WriteLine("星期四");
      }
      else if (s == "Friday")
      {
      Console.WriteLine("星期五");
      }
      else if (s == "Saturday")
     {
     Console.WriteLine("星期六");
     }
     else if (s == "Sunday")
     {
     Console.WriteLine("星期天");
     }
     Console.WriteLine(day);
     Console.ReadLine();

c#:类 相关练习;

标签:

原文地址:http://www.cnblogs.com/Fate-rail/p/4943994.html

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