zoukankan      html  css  js  c++  java
  • C#和.Ne学习第四天

    (主要是传智播客的赵剑宇老师上课的笔记,由于这几天的课和以前上C学C语言和C++几乎都学过所有开始跳着学,在此感谢赵剑宇老师) 

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

    哪行代码有可能出现异常,你就踹它一脚。
    语法:
    try
    {
    可能会出现异常的代码;
    ....
    ...
    ...
    }
    //try和catch之间不能有其他的代码
    catch
    {
    出现异常后要执行的代码;
    }

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


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


    3、switch-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结构
    什么都不做。

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

    }

    特点:先判断,再执行,有可能一遍循环都不执行。
    5、break
    1)、可以跳出switch-case结构。
    2)、可以跳出当前循环。
    break一般不单独的使用,而是跟着if判断一起使用,表示,当满足某些条件的时候,就不再循环了。

    6、do-while循环。
    语法:
    do
    {
    循环体;
    }while(循环条件);
    执行过程:程序首先会执行do中的循环体,执行完成后,去判断do-while循环的循环条件,
    如果成立,则继续执行do中的循环体,如果不成立,则跳出do-while循环。
    特点:先循环,再判断,最少执行一遍循环体。

     练习:

     1 1 2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace ConsoleApplication1
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             int sum = 0;
    15 
    16             for (int t = 1; t <= 100; ++t)
    17             {
    18                 if (t % 2 == 0)
    19                 {
    20                     sum += t;
    21                 }
    22             }
    23 
    24             Console.WriteLine(sum);
    25         }
    26     }
    27 }
    28 输出结果:
    29 2550
    30 请按任意键继续. . .
    31 232 using System;
    33 using System.Collections.Generic;
    34 using System.Linq;
    35 using System.Text;
    36 using System.Threading.Tasks;
    37 
    38 namespace ConsoleApplication1
    39 {
    40     class Program
    41     {
    42         static void Main(string[] args)
    43         {
    44             int sum = 0;
    45 
    46             for (int t = 1; t <= 100; ++t)
    47             {
    48                 if (t % 2 != 0)
    49                 {
    50                     continue;
    51                 }
    52                 else
    53                 {
    54                     sum += t;
    55                 }
    56             }
    57 
    58                 Console.WriteLine(sum);
    59         }
    60     }
    61 }
    62 输出结果:
    63 2550
    64 请按任意键继续. . .
    1
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     class Program
    10     {
    11         public static int Sqrt(int number, int sqrt)
    12         {
    13             int sum = 1;
    14             for (int t = 0; t < sqrt; ++t)
    15             {
    16                 sum *= number;
    17             }
    18 
    19             return sum;
    20         }
    21         static void Main(string[] args)
    22         {
    23             for (int t = 100; t <= 999; ++t)
    24             {
    25                 int a, b, c;
    26                 int countf = 0;
    27                 a = t / 100;
    28                 b = (t / 10) % 10;
    29                 c = t % 10;
    30                 if (t == (Sqrt(a,3) + Sqrt(b,3) + Sqrt(c,3)))
    31                 {
    32                     ++countf;
    33                     Console.WriteLine("{0}:{1}", countf, t);
    34                 }
    35             }
    36         }
    37         
    38     }
    39 }
    2
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication3
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             for (int i = 1; i <= 9; ++i)
    14             {
    15                 for (int j = 1; j <= i; ++j)
    16                 {
    17                     Console.Write("{0}x{1}={2}", i,j,i*j);
    18                 }
    19                 Console.Write("
    ");
    20             }
    21         }
    22     }
    23 }
    3
  • 相关阅读:
    js字符串截取函数slice()、substring()、substr()
    js获取字符串最后一位方法
    支持xhr浏览器:超时设定、加载事件、进度事件
    深入理解ajax系列第一篇——XHR对象
    MySQL命令行操作
    nodejs中mysql用法
    大衍数列
    牌型种数
    加法变乘法
    三羊献瑞
  • 原文地址:https://www.cnblogs.com/2016Study/p/5456639.html
Copyright © 2011-2022 走看看