今天,我先学习了利用try-catch语句异常保护的内容。例如:
try//保护可能出错的语句
{
Console.Write("请输入一个整数:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("输入无误!");
}
catch//若try中的语句有误,会抓住错误,直接执行catch语句
{
Console.WriteLine("您的输入有误!!");
}
finally//不管有没有错,最后都执行
{
Console.WriteLine("感谢使用!");
}
Console.ReadLine();
在学习完异常保护的内容之后,我学习了“类”。在“类”的学习中,主要学习了String类、Math类、随机数类。
在String类中,主要学习了:
string x = Console.ReadLine();
int i = x.Length;//获取长度
Console.WriteLine(i);
Console.Write(x.Trim());//去掉前后空格
Console.Write(x.TrimStart());
Console.Write(x.TrimEnd());
Console.WriteLine(x.ToLower());//大写英文变小写
Console.WriteLine(x.ToUpper());
Console.WriteLine(x.IndexOf("b"));//返回第一次出现该字符或字符串的索引号;索引号是从0开始
Console.WriteLine(x.LastIndexOf("b"));//返回最后一次出现该字符或字符串的索引号
//substring截取字符串
Console.WriteLine(x.Substring(3));
Console.WriteLine(x.Substring(4, 4));
//startswith 是否以**字符串开头
Console.WriteLine(x.StartsWith("ab"));
//endswith 是否以**字符串结尾
Console.WriteLine(x.EndsWith("b"));
//contains 是否包含
Console.WriteLine(x.Contains("cd"));
//replace替换
Console.WriteLine(x.Replace("b", "BB"));
Console.ReadLine();
在Math类的学习中,主要学习了:Ceiling,Floor,Round,Date Time,DayOfYear等部分的内容。
最后,在随机数类Random的学习中,学习了一下Random的用法。
Random ran=new Random();
int a=ran.Next(101);
int b=ran.Next(1,37);//不能取上限,可以取下限