开篇前言:因为年末的关系,工做上事太多,周末又被公司拉着出去应酬,这也许是我这个年龄段要自学一个东西作大的障碍,今天起,加快学习进度。ide
一:异常捕获
异常:语法上没有错误,在程序运行时,由于某些缘由出现错误,致使程序不能正常运行。
为使程序更加健康好用,咱们应该多使用try-catch
语法
try
{
可能出现异常的代码;
}
catch
{
出现异常后执行的代码;
}学习
执行过程:try中的代码没有出现异常,则catch里面不会自行,若是try中代码出现异常,则后面的代码都不执行,直接跳到catch中的代码执行。
Console.WriteLine("输入一串数字");
try
{
int number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的内容,不能被转换为数字!");
}
Console.WriteLine(number 2);
Console.ReadKey();
上面的代码,Console.WriteLine(number 2);里面的Number会报错:变量未赋值,这里就涉及到变量的做用域问题。
变量做用域:
变量的做用域就是你可以使用到这个变量的范围。
变量的做用域通常从声明它的那个括号开始到那个括号所对应的结束的括号结束。
在这个范围内,咱们能够访问并使用变量。超出这个范围就访问不到了code
//try做用域外声明变量,这样就能被访问到,这里还有一个问题,即输出不能被转换的代码后,依然会在输出一个0的结果,由于下面那句是在外面的
int number = 0;//在外面定义变量
Console.WriteLine("输入一串数字");
try
{
number = Convert.ToInt32(Console.ReadLine());//赋值
}
catch
{
Console.WriteLine("输入的内容,不能被转换为数字!");
}
Console.WriteLine(number * 2);//使用
Console.ReadKey();视频
上面的代码因为错误也会多输出一行0,因此须要作以下更改,使输入须要知足必定条件才能。
//声明一个bool变量
bool b = true;
//try做用域外声明变量,这样就能被访问到,这里还有一个问题,即输出不能被转换的代码后,依然会在输出一个0的结果
int number = 0;//声明一个变量
Console.WriteLine("输入一串数字");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的内容,不能被转换为数字!");
b = false;//不知足条件把bool设置为假
}
//要执行这段代码,须要知足必定的条件
//让代码知足某些条件才执行,使用bool变量
if (b)//知足bool值为真才执行
{
Console.WriteLine(number * 2);
}
Console.ReadKey();ci
2、swicth - case
用来处理多条件定值判断
语法
switch(变量或者表达式的值)
{
case 值1:要执行的代码;
break;
case 值2:要执行的代码;
break;
case 值3:要执行的代码;
break;
..........
default:要执行的代码;
break;
}
执行过程:程序执行到switch处,首先将括号中变量或者表达式的值计算出来,
而后拿着这个值依次跟每一个case后面所带的值进行匹配,一旦匹配成功,则执行
该case所带的代码,执行完成后,遇到break。跳出switch-case结构。
若是,跟每一个case所带的值都不匹配。就看当前这个switch-case结构中是否存在
default,若是有default,则执行default中的语句,若是没有default,则该switch-case结构
什么都不作。作用域
if-else-if方法,这个推荐:多条件区间判断使用
bool b = true;
decimal wages = 5000;
Console.WriteLine("输入评级A-E");//A B C D E 乱七八糟
string level = Console.ReadLine();
if (level == "A")
{
wages += 500;
}
else if (level == "B")
{
wages += 200;
}
else if (level == "C")
{
}
else if (level == "D")
{
wages -= 200;
}
else if (level == "E")
{
wages -= 500;
}
else
{
Console.WriteLine("输入有误,退出程序");
b = false;
}
if (b)
{
Console.WriteLine("李四来年工资为{0}", wages);
}
Console.ReadKey();string
switch-case方法,推荐多条件定值判断
bool b = true;
decimal wages = 5000;
Console.WriteLine("输入评级");
string level = Console.ReadLine();
switch (level)
{
case "A":wages += 500;
break;
case "B":wages += 200;
break;
case "C":break;
case "D":wages -= 200;
break;
case "E":wages -= 500;
break;
default:Console.WriteLine("输入有误,退出程序!");
b = false;
break;
}
if (b)
{
Console.WriteLine("李四来年工资为:{0}", wages);
}
Console.ReadKey();it
综合练习:题目:按照用户输入的分数,给出评级,>=90给A,80B,70C,60D,60如下E
int score = 0;
Console.WriteLine("输入一个考试成绩");
try
{
score = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入有误,退出程序");
}
switch (score/10)
{
case 10:
case 9:Console.WriteLine("A级");
break;
case 8:Console.WriteLine("B级");
break;
case 7:Console.WriteLine("C级");
break;
case 6:Console.WriteLine("D级");
break;
default:Console.WriteLine("E级");
break;
}
Console.ReadKey();console
综合练习2:要求输入年份,在输入月份,根据输入给出当月天数
Console.WriteLine("请输入年份");
try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份");
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 29;
}
break;
default:
day = 30;
break;
}
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}//判断月份数字是否正常的if结尾
else
{
Console.WriteLine("输入的月份有误,退出程序");
}
}//月份try的结尾
catch
{
Console.WriteLine("输入的月份有误,退出程序");
}
}//年份try的结尾
catch
{
Console.WriteLine("输入年份有误,退出程序!");
}
Console.ReadKey();class
3、while循坏结构
语法
while(循环条件)
{
循环体;
}
执行过程:程序运行到while处,首先判断while所带的小括号内的循环条件是否成立,
若是成立的话,也就是返回一个true,则执行循环体,执行完一遍循环体后,再次回到
循环条件进行判断,若是依然成立,则继续执行循环体,若是不成立,则跳出while循环。
在while循环当中,通常总会有那么一行代码,可以改变循环条件,使之终有一天再也不成立,
若是没有那么一行代码可以改变循环条件,也就是循环条件永远都成立,咱们称之这种循环
叫作死循环。
最简单的最经常使用的死循环:
while(true)
{
}
特色:先判断,再执行,有可能一遍循环都不执行。
//向控制台打印10遍
//循环体:console.writeLine("一遍又一遍")
//循环条件:打印10遍
//定义一个循环变量来记录循环的次数,每循环一次,变量自增1 int number = 0; while (number < 10) { Console.WriteLine("一遍又一遍{0}",number); number++;//每循环一遍,变量自增1 } Console.ReadKey();
while循环小练习
/
求1-100每一个数加起来的和
循环体 1+2+.....
循环条件:number <=100
/
int i = 1;
int sum = 0;
while (i <= 100)
{
sum += i;
i++;
}
Console.WriteLine(sum);
Console.ReadKey();
/* 求1-100每一个偶数加起来的和 循环体 累加的过程 循环条件:number <=100 */ int i = 1; int sum = 0; while (i <= 100) { if (i % 2 == 0) { sum += i; } i++; } Console.WriteLine(sum); Console.ReadKey(); /* 求1-100每一个奇数加起来的和 循环体 累加的过程 循环条件:number <=100 */ int i = 1; int sum = 0; while (i <= 100) { if (i % 2 != 0) { sum += i; } i++; } Console.WriteLine(sum); Console.ReadKey();
4、break关键字的做用
1)、能够跳出switch-case结构。
2)、能够跳出当前循环。
break通常不单独的使用,而是跟着if判断一块儿使用,表示,当知足某些条件的时候,就再也不循环了。
5、while循环的综合练习
/
输入班级人数,而后一次输入学员成绩,计算班级学员的平均成绩和总成绩
/
//循环体:提示输入学员成绩,转成整数型,累加到总成绩中
//循环条件:输入次数小于等于班级人数
Console.WriteLine("请输入班级人数!");
int sum = 0;
int i = 1;//声明一个变量用来存储循环的次数
int NumberOfPeople = Convert.ToInt32(Console.ReadLine());
while (i<=NumberOfPeople)
{
Console.WriteLine("请输入第{0}成绩!",i);
int score = Convert.ToInt32(Console.ReadLine());
sum += score;//把每一个学员成绩累加到总成绩
i++;
}
Console.WriteLine("平均成绩是{0},总成绩是{1}", sum / NumberOfPeople, sum);
Console.ReadKey();
/
老师问学生,这道题你会作吗?若是学生回答“会了(y)”则能够放学,若是学生回答不会(n),
则老师在讲一遍,在问学生会不会
一、直到学会为止,才能够放学
二、直到学生会或者老师讲10遍还不会,放学
这题第一种是本身想的,反向是根据视频老师的作法
/
string student = "";
while (student != "y")
{
Console.WriteLine("老师讲一遍并问道:你会了吗?");
student = Console.ReadLine();
}
Console.WriteLine("好的,你会了,放学!");
Console.ReadKey();
bool b = true; int second = 0; string student = ""; while (student != "y") { if (second != 10) { Console.WriteLine("老师讲一遍并问道:你会了吗?"); student = Console.ReadLine(); second++; } else { Console.WriteLine("十遍,老师被气死了!"); b = false; break; } } if (b) { Console.WriteLine("好的,会了就放学!"); } Console.ReadKey(); //反向作法 string student = ""; int i = 1; while (student != "y" && i <= 10) { Console.WriteLine("这是老师第{0}几遍讲,你会了吗?", i); student = Console.ReadLine(); if (student == "y") { Console.WriteLine("会了就放学!"); break; } i++; } Console.ReadKey(); /* 2006年培养学员80000人,每一年增加25%,请按照此增加速度,到哪一年培训学员人数达到20万人?PS:这题开始我把判断条件写反了,看了十多分钟才发现问题 */ //循坏体:在原基础上,学员每一年增加25%累加 //循环条件:达到20W double student = 80000; int year = 2006; while (student <= 200000) { Console.WriteLine("如今有{0}人", student); student += Convert.ToInt32(student * 0.25); Console.WriteLine("通过一年有{0}人", student); year++; } Console.WriteLine("{0}年,人数达到20W", year); Console.ReadKey(); /* 提示输入用户名和密码 用户名为admin密码888888 只要用户名或者密码错误就从新输入 最多能输入10此 */ //循环体,让用户输入用户名和密码 //10次,或者输对 int i = 1; string useName = ""; string usePws = ""; while ((useName != "admin" || usePws != "888888")&&i<=3) { Console.WriteLine("输入用户名"); useName = Console.ReadLine(); Console.WriteLine("输入密码"); usePws = Console.ReadLine(); i++; } Console.ReadKey(); 二、第二个循环用户B的用户名不能跟A同样,或者为空, //循环体:提示用户B输入用户名 接收 判断 //用户名为空 或者跟A相同 */ Console.WriteLine("请输入用户名,不能为空"); string userNameA = Console.ReadLine(); while (userNameA == "") { Console.WriteLine("用户名为空,从新输"); userNameA = Console.ReadLine(); } Console.WriteLine("请输入用户名,不能为空"); string userNameB = Console.ReadLine(); while (userNameB == "" || userNameB == userNameA) { if (userNameB == "") { Console.WriteLine("用户名不能为空,从新输入!"); userNameB = Console.ReadLine(); } else { Console.WriteLine("用户名重复,从新输入!"); userNameB = Console.ReadLine(); } }
6、do-while循环结构语法:do{循环体;}while(循环条件);执行过程:程序首先会执行do中的循环体,执行完成后,去判断do-while循环的循环条件,若是成立,则继续执行do中的循环体,若是不成立,则跳出do-while循环。特色:先循环,再判断,最少执行一遍循环体。