Video
传智播客-C#基础-第三天
09 比较三个数字中的最大值
10 try-catch异常捕获
11 三个练习
12 switch-case
13 小结
Note
1.try-catch异常捕获
1 try 2 { 3 可能会出现异常的代码; 4 } 5 catch 6 { 7 出现异常后要执行的代码; 8 }
执行过程:
当try中的出现异常的时候,出现异常的这行代码后面的代码不会执行,而是直接跳到catch中执行catch的代码。
使用规律,哪行代码有可能出现异常,就try一下。
2.变量的作用域
能够访问到这个变量的范围
变量的作用域一般从申明他的那个大括号开始,到这个大括号所对应的结束的大括号
3.如果想要让某些代码在满足某些条件的时候执行,这个时候我们使用bool类型的变量解决
1 using System; 2 using static System.Console; 3 using static System.Convert; 4 5 namespace try-catch 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 int number = 0; 12 WriteLine("请输入 一个数字:"); 13 bool a = true; 14 15 try 16 { 17 number = Convert.ToInt32(ReadLine()); 18 WriteLine("你看我执不执行!!!"); //没有出异常代码执行,否则不执行 19 } 20 catch 21 { 22 Console.WriteLine("输入的字符串不能转换成数字,程序退出"); 23 a = false; 24 } 25 if (a) 26 { 27 WriteLine($"您输入的数字是{number}"); 28 ReadKey(); 29 } 30 31 } 32 } 33 }
4.switch-case
用来做多条件的定值判断
1 switch(要判断的变量或者表达式) 2 { 3 case 值1: 要执行的代码; 4 break; 5 case 值2: 要执行的代码; 6 break; 7 case 值2: 要执行的代码; 8 break; 9 ... 10 default: 要执行的代码 11 break; 12 }
执行过程:
程序运行到switch,首先计算switch后面所带的小括号中的变量或者表达式的值
拿着计算出来的结果跟每个case的值进行匹配,一旦匹配成功,则执行该case所带的代码块
如果跟每个case多带的值不匹配,则看当前switch-case中是否有default
如果有defalut,则执行default所带的代码块,否则的话,什么都不做
Practise
比较三个数字中的最大值
1 using System; 2 using static System.Console; 3 4 namespace 最大值 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 WriteLine("请输入第一个数字:"); 11 int a = Convert.ToInt32(ReadLine()); 12 WriteLine("请输入第二个数字:"); 13 int b = Convert.ToInt32(ReadLine()); 14 WriteLine("请输入第三个数字:"); 15 int c = Convert.ToInt32(ReadLine()); 16 17 if(a > b) 18 { 19 if(a > c) 20 { 21 WriteLine($"最大值是{a}"); 22 } 23 else{ //c>a 24 WriteLine($"最大值是{c}"); 25 } 26 27 } 28 else //b>a 29 { 30 if(b > c) 31 { 32 WriteLine($"最大值是{b}"); 33 } 34 else 35 { 36 WriteLine($"最大值是{c}"); 37 } 38 } 39 ReadKey(); 40 41 /*if(a>b && a>c) 42 { 43 WriteLine($"最大值是{a}"); 44 } 45 else if(b>a && b>c) 46 { 47 WriteLine($"最大值是{b}"); 48 } 49 else 50 { 51 WriteLine($"最大值是{c}"); 52 }*/ 53 } 54 } 55 }
提示用户输入密码,如果密码是"88888"则提示正确
否则要求再输入一次,如果密码是“88888”则提示正确
否则提示错误,程序结束(如果我的代码里有英文还要转换吗?密码:abc1)
1 using System; 2 using static System.Console; 3 4 namespace 练习1 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 WriteLine("请输入密码:"); 11 string number = ReadLine(); 12 if(number == "88888") 13 { 14 WriteLine("您输入的密码正确"); 15 } 16 else 17 { 18 WriteLine("您输入的密码错误,请再次输入"); 19 number = ReadLine(); 20 if (number == "88888") 21 { 22 WriteLine("您输入的密码正确"); 23 } 24 } 25 ReadKey(); 26 } 27 } 28 }
提示用户输入用户名,然后提示输入密码,如果用户名是"admin"并且密码是“88888”,则提示以正确
否则,如果用户名不是“admin”还提示用户用户名不存在
如果用户名是admin,则提示密码错误
1 using System; 2 using static System.Console; 3 4 namespace 练习2 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 WriteLine("请输入你的用户名: "); 11 string account = ReadLine(); 12 WriteLine("请输入你的密码: "); 13 string number = ReadLine(); 14 15 if (account == "admin" && number == "88888") 16 { 17 WriteLine("正确"); 18 } 19 else if (number == "88888") 20 { 21 WriteLine("您的用户名不存在"); 22 } 23 else if(account == "admin") 24 { 25 WriteLine("密码错误"); 26 } 27 else 28 { 29 WriteLine("用户名和密码都错啦!"); 30 } 31 ReadKey(); 32 } 33 } 34 }
提示用户输入年龄,如果大于等于18,则告诉用户可以查看
如果小于10岁,则告知不允许查看
如果大于等于10岁并且小于18岁,则提示用户是否继续查看(yes,no)
如果输入yes则提示用户请查看,否则提示“退出,你放弃查看”
1 using System; 2 using static System.Console; 3 4 5 namespace ConsoleApp10 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 WriteLine("请输入你的年龄: "); 12 int age = Convert.ToInt32(ReadLine()); 13 if(age > 18) 14 { 15 WriteLine("你可以查看"); 16 } 17 else if(age > 10) 18 { 19 WriteLine("你是否查看,(yes,no)"); 20 string number = ReadLine(); 21 if(number == "yes") 22 { 23 WriteLine("你可以查看"); 24 } 25 else 26 { 27 WriteLine("你选择了不查看"); 28 } 29 } 30 else 31 { 32 WriteLine("你不可以查看"); 33 } 34 ReadKey(); 35 } 36 } 37 }
李四的年终工作评定
如果定为A级,则工资涨500元
如果定为B级,则工资涨200元
如果定为C级,则工资不变
如果定为D级,则工资降200元
如果定为E级,则工资降500元
设李四的原工资为5000元,请用户输入李四的评级,然后显示李四来年的工资
1 using System; 2 using static System.Console; 3 4 namespace ConsoleApp11 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 WriteLine("请输入李四的评级: "); 11 string level = ReadLine(); 12 double salary = 5000; 13 bool b = true; 14 if(level == "A") 15 { 16 salary += 500; 17 } 18 else if(level == "B") 19 { 20 salary += 200; 21 22 } 23 else if (level == "C") 24 { 25 26 } 27 else if (level == "D") 28 { 29 salary -= 200; 30 31 } 32 else if (level == "E") 33 { 34 salary -= 500; 35 36 } 37 else 38 { 39 WriteLine("输入错误,请重新输入"); 40 b = false; 41 } 42 if (b) 43 { 44 WriteLine($"李四明年的工资是{salary}"); 45 } 46 ReadKey(); 47 } 48 } 49 }