• Day.23


    2019.11.14

    Video

    传智播客-C#基础-第三天

    09 比较三个数字中的最大值

    10 try-catch异常捕获

    11 三个练习

    12 switch-case

    13 小结

     传智播客-C#基础-第三天

    01 复习

    02 switch-case 练习

    Note

    1.try-catch异常捕获

    1 try
    2             {
    3                 可能会出现异常的代码;
    4             }
    5 catch
    6             {
    7                 出现异常后要执行的代码;
    8             }
    try-catch

    执行过程:

    当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 }
    try-catch

    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-case

    执行过程:

    程序运行到switch,首先计算switch后面所带的小括号中的变量或者表达式的值

    拿着计算出来的结果跟每个case的值进行匹配,一旦匹配成功,则执行该case所带的代码块

    如果跟每个case多带的值不匹配,则看当前switch-case中是否有default

    如果有defalut,则执行default所带的代码块,否则的话,什么都不做

     5.总结

    1)算数运算符 ++(前/后)--(前/后)

    2)复合赋值运算符 += -= *= /= %=

    3)关系运算符 > < >= <= == !=

    4)逻辑运算符 && || !

    5)布尔类型 bool

    6)分支结构 if结构 if-else机构

    7)选择结构 if else-if结构(判断多条件区间) switch-case结构(判断多条件定值)

    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 }
    88888

    提示用户输入用户名,然后提示输入密码,如果用户名是"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 }
    admin

    提示用户输入年龄,如果大于等于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 }
    18

    李四的年终工作评定

    如果定为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 }
    5000
     1 using System;
     2 using static System.Console;
     3 
     4 namespace ConsoleApp12
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             WriteLine("请输入李四的年终评定:");
    11             string level = ReadLine();
    12             int salary = 5000;
    13             bool b = true;
    14             switch (level)
    15             {
    16                 case "A": salary += 500;
    17                     break;
    18                 case "B": salary += 200;
    19                     break;
    20                 case "C":
    21                     break;
    22                 case "D": salary -= 200;
    23                     break;
    24                 case "E": salary -= 500;
    25                     break;
    26                 default: WriteLine("输入有误!");
    27                     b = false;
    28                     break;
    29             }
    30             if (b)
    31             {
    32                 WriteLine($"李四明年的工资是{salary}");
    33             }
    34             ReadKey();
    35         }
    36     }
    37 }
    5000

    输入这几个人的名字,计算这几个人上辈子都是干什么的?

    (老杨、老苏、老邹、老牛、老蒋、老王、老马)

     1 using System;
     2 using static System.Console;
     3 
     4 namespace ConsoleApp1
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             Console.WriteLine("请输入要计算的人的姓名");
    11             string name = ReadLine();
    12 
    13             switch(name)
    14             {
    15                 case "老杨": WriteLine("老杨上辈子是折翼的天使");
    16                     break;
    17                 case "老苏": WriteLine("老苏上辈子是老鸨子");
    18                     break;
    19                 case "老邹": WriteLine("老邹上辈子是老苏手下的偷拍");
    20                     break;
    21                 case "老虎": WriteLine("上辈子被武松挂了");
    22                     break;
    23                 case "老牛": WriteLine("上辈子是cow");
    24                     break;
    25                 default: WriteLine("上辈子没这个人");
    26                     break;
    27             }
    28             ReadKey();
    29         }
    30     }
    31 }
    上辈子

    对员工的结业成绩评测

     1 using System;
     2 using static System.Console;
     3 
     4 namespace ConsoleApp2
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             WriteLine("请输入考试成绩");
    11             int score = Convert.ToInt32(ReadLine());
    12 
    13             switch(score/10)
    14             {
    15                 case 10 : WriteLine("A"); //当连续几个的case代码一致,前面代码可以删除
    16                     return;
    17                 case 9: WriteLine("A");
    18                     return;
    19                 case 8: WriteLine("B");
    20                     return;
    21                 case 7: WriteLine("C");
    22                     return;
    23                 case 6: WriteLine("D");
    24                     return;
    25                 default: WriteLine("E");
    26                     return;
    27             }
    28             ReadKey();
    29         }
    30     }
    31 }
    Score

      

  • 相关阅读:
    超级变态之access查询
    计算机安全病毒问题汇集(转自华军)
    Avast I Love You
    Windows2003 3389端口修改
    希捷 250G 7200.10 8M(串口/5年盒)(买硬盘了~~~)
    DataTable 中Remove方法的使用
    我的主板(p5pl2e)
    冼东妹(为奥运冠军名字作诗)
    李彦宏告诫年轻人:向前看两年
    郭文珺(为奥运冠军名字作诗)
  • 原文地址:https://www.cnblogs.com/vsdd/p/11854915.html
Copyright © 2020-2023  润新知