using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; //关于线程的,不加不能实现线程休息 namespace _8._7课堂随笔 { class Program { static void Main(string[] args) { int num; do try { { Console.WriteLine("\n请输入序号\n1.Math类几个实例 \n2.时间,日期类型\n3.TimeSpan的使用\n4.DateTime的录入"); Console.WriteLine("0为退出"); Console.WriteLine(DateTime.Now); //顺便加一个时间的显示 num = int.Parse(Console.ReadLine()); //强制赋值为整形,如果输入非整形会导致BUG,可以进行一次判断 if (num == 0) { Console.WriteLine("再见!"); return; } else { switch (num) { case 1: double pi = Math.PI; Console.WriteLine(pi); double a = Math.Ceiling(1.2343); //小数取整+1,取上限 Console.WriteLine(a); double b = Math.Floor(1.2343); //小数取整,取下限 Console.WriteLine(b); double c = Math.Round(5.5); //四舍五入 用"."这用就是静态方法 Random X = new Random(); //面向对象时,实例化(构造一个) random后括号内需要种子,不填默认是时间种子,为什么会要种子 Console.WriteLine(X.Next(0, 20)); break; case 2: DateTime time = DateTime.Now; //计算机当前的时间,输出采用默认格式, 注意:这里定义后循环中的时间不会改变,如需实时改变需要在循环中重新定义一次 Console.WriteLine(time); DateTime dt = DateTime.Today; // Console.WriteLine(dt); Console.WriteLine("当前时间" + time.ToString("yyyy-MM-dd hh:mm:ss")); //注意输出的格式,MM表示月份,mm表示分钟 int year = time.Year; //充足的参数,可以最多取到毫秒级 int month = time.Month; int day = time.Day; int m = time.Hour; int s = time.Minute; int ms = time.Millisecond; int ss = time.Second; for (int i = 9; i < 100; i++) { time = DateTime.Now; year = time.Year; //充足的参数,可以取到毫秒级 month = time.Month; day = time.Day; m = time.Hour; s = time.Minute; ms = time.Millisecond; ss = time.Second; //重新赋值实现实时改变 Console.WriteLine(year + "年" + month + "月" + day + "日" + m + "时" + s + "分" + ss + "秒" + ms + "毫秒"); Thread.Sleep(2000);// 线程休息2000毫秒 } break; case 3: DateTime time1 = DateTime.Now; TimeSpan spantime = new TimeSpan(1, 1, 1, 1, 1); //TimeSpan定义的时间是(天,小时,分钟,秒,毫秒) DateTime time2 = time1.Add(spantime); Console.WriteLine(time2); TimeSpan spantime2 = time2 - time1; Console.WriteLine(spantime2); if (spantime == spantime2) Console.WriteLine("Y"); Console.WriteLine("总的小时数" + spantime.TotalHours); //“.”后加属性 可以时间计算总小时数,或者秒数等 break; case 4: DateTime nDT = DateTime.Parse("1989-10-5"); //字符串转时间 Console.WriteLine(nDT); DateTime nDt = Convert.ToDateTime("1989.10.5"); Console.WriteLine(nDT); break; case 5: Console.WriteLine("5"); break; case 6: Console.WriteLine("6"); break; default: Console.WriteLine("输入错误序号,大侠请再来一次"); break; } } } } catch (Exception ex) { Console.WriteLine("错误是" + ex.Message); Console.ReadLine(); } while (0== 0); //序号num不为0时循环 } } }
时间: 2024-10-10 21:06:14