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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值