0914 练习题,类类型(用户自定义类型)

一,输入您的身份证号,识别是哪一年几月几日出生,并计算出你的年龄

Console.Write("请输入您的身份证号:"); // 输出一串字符“请输入您的身份证号”

string s = Console.ReadLine(); // 把身份证这串字符定义在 s 这个变量里面  等待你输入(录入)

string n = s.Substring(6,4); // 定义 n 等于 s里面从第6位开始数 后面的 4位数字

string y = s.Substring(10,2); // 定义 y 等于 s里面从第10位开始数 后面的 2位数字

string r = s.Substring(12, 2); // 定义 y 等于 s里面从第12位开始数 后面的 2位数字

Console.WriteLine("您的出生日期是{0}年{1}月{2}日",n,y,r); // 输出 “您的出生日期是哪一年哪一月哪一日”

int shu = int.Parse(n);  // 把 n 转换成 int类型  shu

int j = 2015;  // 定义 j 等于 2015

int sui=j-shu; // 定义 sui 等于 2015 - n

Console.WriteLine("您今年{0}岁了",sui);   // 输出 你今年 多少 岁了

二,生产一个随机数,显示随机中奖号码

Random r = new Random();
for (int i = 0; i <= 100; i++)
{
   int shu = r.Next(1001, 9999);
    Console.WriteLine(shu);
    Thread.Sleep(100);  // 延时,单位毫秒
    Console.Clear();
}
Console.WriteLine("中奖号码是:1002");

三,抓取错误

int cuo = 0; // 定义一个变量
Console.WriteLine("请输入日期时间"); //输出一段文字“请输入时间日期”
string s = Console.ReadLine();
try
{
    DateTime dt = DateTime.Parse(s); //将字符串类型转换为时间日期类型
}
catch (Exception ex) //抓获错误
    {
        Console.WriteLine("错误");
        cuo=1;                         //如果 变量 cuo =1  那就是错误
    }
if (cuo==0)                             // 如果 变量 cuo = 0 那就是正确
{
    Console.WriteLine("正确");
}

四,分割

string s = "a|b|c|ab|cd|c|d";            

//string[] str = s.Split('|');            

//foreach (string d in str)            

//{

           

//    Console.WriteLine(d);                        

//}                                    

//string s = "a|b|c|ab|cd|c|d";            

//string[] str = s.Split('|');            

//Console.WriteLine(str[0]);            

//Console.WriteLine(str[1]);            

//Console.WriteLine(str[2]);            

//Console.WriteLine(str[3]);            

//Console.WriteLine(str[4]);            

//Console.WriteLine(str[5]);            

//Console.WriteLine(str[6]);

五,

// Math : 里面有些处理数字的方法,静态方法
            //int i = Math.Abs(-5); //取绝对值
            //Console.WriteLine(i);
            //double a = Math.Ceiling(1.1); //天花板
            //Console.WriteLine(a);
            //Math.Floor(1.9); // 下限
            //Console.WriteLine(Math.PI); //圆周率
            //Console.WriteLine(Math.Round(1.41)); // 四舍五入
            //Console.WriteLine(Math.Pow(2,3)); // 慕次方
            //Console.WriteLine(Math.Sqrt(25)); // 平方根

原文地址:https://www.cnblogs.com/jlhea/p/4809210.html