C#学习-Day6

异常捕获

我们在程序中经常会出现各种各样的异常,你如果想要你的程序变得坚强些,在你的代码中应该经常性的使用try-catch来进行异常捕获

语法:

try

{

可能会出现异常的代码;

...........

...........

}

catch

{

出现异常后要执行的代码;

}

执行过程:如果try中的代码没有出现异常,那么catch中的代码不会执行。如果try中的代码出现了异常,哪怕这行出现异常的代码后面还有一百行都不会执行了,而是直接跳到catch中执行代码,try和catch之间不能有其它代码;

变量的作用域

变量 的作用域就是你能够使用到这个变量的范围,变量的作用域一般从声明它的那个括号开始到那个括号所对应的括号结束,在这个范围内我们可以访问并使用这个变量,超出了这个范围就访问不到了

            bool b = true;
           int number = 0;
           Console.WriteLine("请输入一个数字");
           try
          {
               number = Convert.ToInt32(Console.ReadLine());
          }
           catch
          {
               Console.WriteLine("输入的内容不能转换成数字");
               b = false;
          }
           if (b)
          {
               Console.WriteLine(number*2);
          }
           Console.ReadKey();

switch-case

用来处理多条件的定值判断

语法:

switch(变量或者表达式的值)

{

case值1:要执行的代码;

break;

case值1:要执行的代码;

break;

case值1:要执行的代码;

break;

...............

default:要执行的代码;

break;

}

执行过程:程序执行到switch处,首先将括号中变量或者表达式的值计算出来,然后拿着这个值依次跟每个case后面所带的值进行匹配,一旦匹配成功,则执行该case所带的代码,执行完成后,遇到break,跳出switch-case结构,如果这个值跟每个case所带的值都不匹配,就看当前这个switch-case结构中是否存在default,如果有default,则执行default中的语句,如果没有default,则该switch-case结构什么都不做

            int salary = 5000;
           bool b = true;
           string level = Console.ReadLine();
           switch (level)
          {
               case "A": salary += 500;
                   break;
               case "B": salary += 200;
                   break;
               case "C": break;
               case "D": salary -= 200;
                   break;
               case "E": salary -= 500;
                   break;
               default: Console.WriteLine("输入有误,程序终止");
                   b = false;
                   break;
          }
           if (b)
          {
               Console.WriteLine("某人的工资是{0}", salary);
          }
           Console.ReadKey();
            Console.WriteLine("请输入一个姓名,我们将显示出来这个人上辈子的职业");
           string name = Console.ReadLine();
           switch(name)
          {
               case "刘": Console.WriteLine("医生");
                   break;
               case "王": Console.WriteLine("老师");
                   break;
               case "罗": Console.WriteLine("学者");
                   break;
               case "李": Console.WriteLine("公务员");
                   break;
               case "张": Console.WriteLine("商人");
                   break;
               default: Console.WriteLine("查无此人");
                   break;
          }
           Console.ReadKey();
            Console.WriteLine("请输入你的考试成绩");
           int score = Convert.ToInt32(Console.ReadLine());
           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.WriteLine("请输入年份");
           try
          {
               int year = Convert.ToInt32(Console.ReadLine());
               Console.WriteLine("请输入月份");
               int month = Convert.ToInt32(Console.ReadLine());
               switch (month)
              {
                   case 1:
                   case 3:
                   case 5:
                   case 7:
                   case 8:
                   case 10:
                   case 12: Console.WriteLine("本月有31天");
                       break;
                   case 4:
                   case 6:
                   case 9:
                   case 11: Console.WriteLine("本月有30天");
                       break;
                   case 2:
                       if ((year % 4 == 0) || (year / 4 == 0 && year / 100 != 0))
                      {
                           Console.WriteLine("本月有29天");
                      }
                       else
                      {
                           Console.WriteLine("本月有28天");
                      }
                       break;
                   default: Console.WriteLine("输入格式错误,程序结束");
                       break;
              }
          }
           catch
          {
               Console.WriteLine("格式错误,程序终止");
          }
           Console.ReadKey();

循环结构

while循环

while(循环结构)

{

循环体;

}

特点:先判断后执行,可能一遍循环也不执行

执行过程:程序运行到 while处,首先判断while所带的小括号内的循环条件是否成立,如果成立的话,也就是返回一个true,则执行循环体,执行完一遍循环体后,再次回到循环条件进行判断,如果依然成立,则继续执行循环体,如果不成立,则跳出while循环。在while循环中,一般会有那么一行代码,能够改变循环条件,使之终有一天不再成立,如果没有那么一行代码能够改变循环条件,也就是循环条件永远都成立,我们称这种循环为死循环

最简单最常用的死循环:

while(true)

{

}

            int n = 0;
           while (n < 100)
          {

               n++;
               Console.WriteLine("下次考试我一定细心\t{0}", n);
          }
           Console.ReadKey();
            int n = 0;
           int sum = 0;
           while (n < 100)
          {
               n++;
               sum += n;
          }
           Console.WriteLine(sum);
           Console.ReadKey();
            Console.WriteLine("请输入班级人数");
           try
          {
               int count = Convert.ToInt32(Console.ReadLine());
               int i = 0;
               int sum = 0;
               while (i < count)
              {
                   i++;
                   Console.WriteLine("请输入第{0}个学员的成绩", i);
                   int score = Convert.ToInt32(Console.ReadLine());
                   sum += score;
              }
               Console.WriteLine("{0}个人的总成绩是{1},平均成绩是{2}", count, sum, sum / count);
          }
           catch
          {
               Console.WriteLine("输入错误,程序终止");
          }
           Console.ReadKey();
            Console.WriteLine("这道题会了吗?");
            string answer = Console.ReadLine();
            int i = 1;
            while (answer != "yes" && i <=9)
            {
                i++;

                Console.WriteLine("这是第{0}遍了,会了吗?",i);
                answer = Console.ReadLine();
                if (answer == "yes")
                {
                    Console.WriteLine("会了就放学");
                }
            }
            Console.ReadKey();
            int year = 2006;
            double people = 80000;
            while (people < 200000)
            {
                people *= 1.25;
                year++;
            }
            Console.WriteLine("到第{0}年的时候人数将达到20万人",year);
            Console.ReadKey();

break

  1. 可以跳出当前循环

  2. break一般不单独使用,可以跟着if判断一起使用,表示满足某些条件的时候就不再循环了

do-while

语法:

do

{

循环体;

}while(循环条件);

执行过程:程序首先会执行do中的循环体,执行完成后,去判断do-while循环的循环条件,如果成立,则继续执行do中的循环体,如果不成立,则跳出do-while循环

特点:先循环,后判断,做少执行一道循环体

           string answer="";
            do
            {
                Console .WriteLine("老师,我唱的您满意吗?");
                answer=Console.ReadLine();
            }while (answer!="yes");
            Console.WriteLine("OK,放学回家");
            Console.ReadKey();
            string administor = "";
            string password = "";
            Console.WriteLine("请输入用户名");
            administor = Console.ReadLine();
            if (administor != "admin")
            {
                do
                {
                    Console.WriteLine("请重新输入用户名");
                    administor = Console.ReadLine();
                } while (administor != "admin");
            }
            Console.WriteLine("请输入密码");
            password = Console.ReadLine();
            if (password != "88888")
            {
                do
                {
                    Console.WriteLine("请重新输入密码");
                    password = Console.ReadLine();
                } while (password != "88888");
            }
            Console.WriteLine("登录成功");
            Console.ReadKey();
            Console.WriteLine("请输入一个数字,我们将打印这个数字的两倍");
            string input = Console.ReadLine(); 
            if (input != "q")
            {
                do
                {     
                    try
                    {
                        int number = Convert.ToInt32(input);
                        Console.WriteLine("您输入的数字的两倍是{0}", number * 2);
                        Console.WriteLine("请输入一个数字,我们将打印这个数字的两倍");
                        input = Console.ReadLine();
                    }
                    catch
                    {
                        Console.WriteLine("输入的字符串不能组成数字,请重新输入");
                        input = Console.ReadLine();
                    }
                } while (input != "q");
            }  
            else
            {
                Console.WriteLine("输入的是q,程序终止");
            }
            Console.ReadKey();

 

posted @ 2022-02-28 22:37  肥肥的苹果  阅读(183)  评论(0)    收藏  举报