【2017-12-07】c#基础-String类、Math类、DateTime类
String类:
Math类:
DateTime类:
练习:
1、用户输入一个数字
输出:1,2,3,4,5,6
1 Console.Write("请输入一个数字:"); 2 int a = Convert.ToInt32(Console.ReadLine()); 3 string b=""; 4 for (int i = 1; i <= a; i++) 5 { 6 b=b+i+","; 7 } 8 b = b.Substring(0,2*a-1);//此题只考虑10以内,10以上每个数字占多个字符 9 Console.WriteLine(b);
2、“请输入身份证号(18位):”
判断正确性:
全数字
最后一位x/X
中间时间是否正确
正确输出:“您的生日是:2000年1月1日”
错误输出:“您的输入有误”
1 Console.Write("请输入身份证号:"); 2 string s = Console.ReadLine();//存身份证 3 bool b = true;//存放身份证格式是否正确 4 DateTime d = DateTime.Now;//存放生日 5 if (s.Length != 18) 6 { 7 b = false; 8 } 9 else 10 { 11 if (s.Substring(17, 1) == "X" || s.Substring(17, 1) == "x") 12 { 13 try 14 { 15 long a = Convert.ToInt64(s.Substring(0, 17)); 16 } 17 catch 18 { 19 b = false; 20 } 21 } 22 else 23 { 24 try 25 { 26 long a = Convert.ToInt64(s); 27 } 28 catch 29 { 30 b = false; 31 } 32 } 33 if (b == true) 34 { 35 try 36 { 37 d = Convert.ToDateTime(s.Substring(6, 4)+","+s.Substring(10,2)+","+s.Substring(12,2)); 38 } 39 catch 40 { 41 b = false; 42 } 43 } 44 } 45 if (b == true) 46 { 47 Console.WriteLine("您的生日是:" + s.Substring(6, 4) + "年" + s.Substring(10, 2) + "月" + s.Substring(12, 2)+"日"); 48 } 49 else 50 { 51 Console.WriteLine("您的输入有误!"); 52 }
3、“请输入年:”
“请输入月:”
“请输入日:”
判断是否正确
“xxxx年xx月xx日是此年中的第xx天,星期几”
“距离2012年12月24日世界末日还有xxx天”
“距离2012年12月24日世界末日已经过去了xxx天”
“您输入的是世界末日!!!”
1 Console.Write("请输入年:"); 2 string n = Console.ReadLine(); 3 Console.Write("请输入月:"); 4 string y = Console.ReadLine(); 5 Console.Write("请输入日:"); 6 string r = Console.ReadLine(); 7 bool b; 8 DateTime d = new DateTime(); 9 try 10 { 11 d = Convert.ToDateTime(n + "," + y + "," + r); 12 b = true; 13 } 14 catch 15 { 16 b = false; 17 } 18 if (b == false) 19 { 20 Console.WriteLine("输入有误!"); 21 } 22 else 23 { 24 string xq = "日一二三四五六"; 25 Console.WriteLine("\n"+n+"年"+y+"月"+r+"日是此年中的第"+d.DayOfYear+"天,星期"+xq.Substring(Convert.ToInt16(d.DayOfWeek),1)+"\n"); 26 DateTime mr = new DateTime(2012,12,24); 27 TimeSpan ts = d - mr; 28 int a = ts.Days; 29 if (a == 0) 30 { 31 Console.WriteLine("您输入的是世界末日!!!"); 32 } 33 else if (a < 0) 34 { 35 Console.WriteLine("距离2012年12月24日世界末日还有" + a + "天"); 36 } 37 else 38 { 39 Console.WriteLine("距离2012年12月24日世界末日已经过去了" + a + "天"); 40 } 41 }
4、“请输入您的邮箱:”
1-“只能有一个@符号”
2-“不能以@开头”
3-“不能以@结尾”
4-“@之后必须有点”
5-“@之后不能是点”
6-最少一个点,最多两个点
7-“不能以点结尾”
8-不能以数字结束
1 // “请输入您的邮箱:” 2 Console.Write("请输入您的邮箱:"); 3 string yx = Console.ReadLine(); 4 bool b = false; 5 //1 -“只能有一个 @符号” 6 if (yx.Contains("@") && yx.IndexOf("@", yx.IndexOf("@") + 1) == -1) 7 { 8 //2 -“不能以 @开头” 9 if (!yx.StartsWith("@")) 10 { 11 //2 -“不能以 @开头” 12 if (!yx.EndsWith("@")) 13 { 14 //4 -“@之后必须有点” 15 if (yx.IndexOf(".", yx.IndexOf("@") + 1) != -1) 16 { 17 //5 -“@之后不能是点” 18 string x = yx.Substring(yx.IndexOf("@")+1,1); 19 if (x != ".") 20 { 21 //6 - 最少一个点,最多两个点 22 int a1=-1; 23 int count = 0; 24 while (true) 25 { 26 a1 = yx.IndexOf(".",a1+1); 27 if (a1 == -1) 28 break; 29 else 30 count++; 31 } 32 if (count == 1 || count == 2) 33 { 34 //7 -“不能以点结尾” 35 if (!yx.EndsWith(".")) 36 { 37 //8 - 不能以数字结束 38 string s = yx.Substring(yx.Length-1,1); 39 try 40 { 41 //最后一位可以转为数字则错误,不能出错则正确 42 int s1 = Convert.ToInt32(s); 43 } 44 catch 45 { 46 b = true; 47 } 48 } 49 } 50 } 51 } 52 } 53 } 54 } 55 if (b == true) 56 { 57 Console.Write("输入正确!你的邮箱是:" + yx+"."); 58 } 59 else 60 { 61 Console.Write("输入错误!"); 62 }