//异常语句 try catch finally:
try //保护执行里面的代码段,如果其中有一句错误,直接跳到catch,不会管下面的内容
{
Console.Write("请输入一个整数:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("您好");
}
catch //try中间发生异常直接执行,若无错误,不执行
{
Console.WriteLine("输入有误");
}
finally //不管上面有没有错误都执行
{
Console.WriteLine("谢谢!");
}
Console.ReadLine();
重新输入:(几个比较好玩的题)
1、一只问你到底爱不爱我,直到回答说爱为止,然后回答我也爱你。
for (; ; ) { Console.Write("你到底爱不爱我呀!"); string a = Console.ReadLine(); Console.WriteLine(); if (a == "爱") { Console.Write("我也爱你!!"); break; } else { } } Console.ReadLine();
2. 碰到输入有误,再次从新输入:
for (int i = 1; i <= 3; i++) { if (i == 1) { Console.Write("你到底爱不爱我呀!"); } if (i == 2) { Console.Write("我在问你呢,你到底爱不爱我呀!"); } if (i == 3) { Console.Write("你没听到吗,我在问你你到底爱不爱我呀!"); } string a = Console.ReadLine(); Console.WriteLine(); if (a == "爱") { Console.Write("我也爱你!"); Console.WriteLine(); System.Threading.Thread.Sleep(1000); //停顿一秒再出现下句话。 Console.WriteLine("从此他们幸福的生活在一起啦!"); } else { if (i == 3) { Console.WriteLine("我再也不想理你了!"); } } } Console.WriteLine(); Console.ReadLine();
3、 输入年月日,看看格式是否正确,如果输入错误,则再次重新输入。
for (; ; ) { Console.Write("请输入年份:"); int year = int.Parse(Console.ReadLine()); if (year >= 0 && year <= 9999) { for (; ; ) { Console.Write("请输入月份:"); int month = int.Parse(Console.ReadLine()); if (month >= 1 && month <= 12) { for (; ; ) { Console.Write("请输入日期:"); int day = int.Parse(Console.ReadLine()); if (day >= 1 && day <= 31) { if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { Console.WriteLine("现在是" + year + "年" + month + "月" + day + "日,格式正确!"); } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (day <= 30) { Console.WriteLine("现在是" + year + "年" + month + "月" + day + "日,格式正确!"); } else { Console.WriteLine("您输入的日期有误,请重新输入!"); } } else//month=2 { if(day<=28) { Console.WriteLine("现在是" + year + "年" + month + "月" + day + "日,格式正确!"); } else { if(day==29) { if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0) { Console.WriteLine("现在是" + year + "年" + month + "月" + day + "日,格式正确!"); } else { Console.WriteLine("您输入的日期有误,请重新输入!"); } } } } break; } else { Console.WriteLine("您输入的日期有误,请重新输入!"); } } break; } else { Console.WriteLine("您输入的月份有误,请重新输入!"); } } break; } else { Console.WriteLine("您输入的年份有误,请重新输入!"); } } Console.ReadLine();
//string 类:
string a = " asdfgHVj88";
int b = a.Length; //长度 属性不用加();方法要加()
Console.WriteLine(b);
string c = a.Trim(); //去掉前后空格
Console.WriteLine(c);
string d = a.TrimStart(); //去掉前空格
Console.Write("
"); //空格
Console.WriteLine(d);
string e = a.TrimEnd(); //去掉后空格
string f = a.ToUpper(); //全部将字母小写换为大写(验证码转换)
string g = a.ToLower(); //全部将字母大写换为小写
//索引号是从0开始的
string h = a.Substring(4); //里面那一个值,表示从这个开始索引到最后
Console.WriteLine(h);
Console.WriteLine(a.Substring(8));//不重新赋值,a是没有变化的
string i = a.Substring(4, 3); //两个值表示从哪个索引号开始截取多少长度
Console.WriteLine(i);
string j = a.Replace("df", "DF"); //替换大小写
Console.WriteLine(j);
string k = "2012/12/23";
string[] aa = k.Split(); //分割字符串,以什么字符。
bool y = a.Contains("ff"); //判断是否包含此字符
bool n = a.StartsWith("a"); //是否以此字符串为开头,返回True或False
bool m = a.EndsWith("a"); //是否以此字符串为结尾,返回True或False
int z = a.IndexOf("d"); //从前面开始找到第一个d,从前往后数他的索引号
int w = a.LastIndexOf("d"); //从后面开始找到第一个d,从前往后数他的索引号
//math类:
double s = 4.145;
Console.WriteLine(Math.Ceiling(s)); //取上线
Console.WriteLine(Math.Floor (s)); //取下线
Console.WriteLine (Math.PI*s); // π圆周率
Console.WriteLine(Math.Sqrt(s)); //开平方根
Console.WriteLine(Math.Round(s)); //四舍五入
//奇数5的情况下,取上线
//偶数5的情况下,取下线
// 练习:判断邮箱格式是否正确
1.有且只能有一个@
2.不能以@开头
3.@之后至少有一个.
4.@和.不能靠在一起
5.不能以.结尾