zoukankan      html  css  js  c++  java
  • C# 课堂总结3-语句

    一、顺序语句

    二、条件,分支语句

    1、if语句

    关键是能够熟练运用 if的嵌套。要考虑好所有的情况。

    如果说 条件是两种+情况相互对应的,那么就可以只用 if 与else 。但必须要想好 每个else 跟哪个if是一对。

    如果情况是相互独立的三种情况以上,那么可以选择运用if ... else if ...else。

    1.if语句

    if(条件)
    {
    满足条件的时候执行;
    }

    2. if(条件)

    {
    满足条件执行;
    }
    else
    {
    不满足条件时执行;
    }

    3 if(条件1)
    {
    满足条件1的时候执行;
    }
    else if(条件2)
    {
    不满足条件1的情况下满足条件2;
    }

    4.

    if(条件1)
    {
    if(条件2)
    {
    既满足条件1又满足条件2的时候执行;
    }
    }

    eg 1.判断年份是否为闰年?格式是否正确?

        static void Main5(string[] args)
            {
                Console.WriteLine("请输入年份:");
                int nian = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine("请输入月份:");
                int yue = Convert.ToInt32(Console.ReadLine());
    
                Console.WriteLine("请输入日期:");
                int ri = Convert.ToInt32(Console.ReadLine());
    
                int r = 0; //r=0代表平年 r=1代表闰年
    
                if (nian > 0 && nian < 9999)
                {
                    //判断闰年还是平年
                    if (nian % 100 == 0)
                    {
                        if (nian % 400 == 0)
                        {
                            r = 1;
                            Console.WriteLine("该年是闰年");
                        }
                        else
                        {
                            Console.WriteLine("该年是平年");
                        }
                    }
                    else
                    {
                        if (nian % 4 == 0)
                        {
                            r = 1;
                            Console.WriteLine("该年是闰年");
                        }
                        else
                        {
                            Console.WriteLine("该年是平年");
                        }
                    }
    
                    //判断月份
                    if (yue >= 1 && yue <= 12)
                    {
                        //判断日期是否合法
                        if(yue==1 || yue==3 || yue == 5 || yue==7 || yue==8 || yue==10 ||yue == 12)
                        {
                            if(ri<=31 && ri>0)
                            {
                                Console.WriteLine("输入的日期正确!");
                            }
                            else
                            {
                                Console.WriteLine("输入的日期不正确!");
                            }
                        }
                        else if(yue == 4 || yue == 6 || yue==9 || yue==11)
                        {
                            if(ri<=30 && ri>0)
                            {
                                Console.WriteLine("输入的日期正确!");
                            }
                            else
                            {
                                Console.WriteLine("输入的日期不正确!");
                            }
                        }
                        else
                        {
                             if(r == 1)
                             {
                                 if(ri>0 && ri<=29)
                                 {
                                     Console.WriteLine("输入的日期正确!");
                                 }
                                 else
                                 {
                                     Console.WriteLine("输入的日期不正确!");
                                 }
                             }
                             else
                             {
                                  if(ri>0 && ri<=28)
                                 {
                                     Console.WriteLine("输入的日期正确!");
                                 }
                                 else
                                 {
                                     Console.WriteLine("输入的日期不正确!");
                                 }
                             }
                        }
    
                    }
                    else
                    {
                        Console.WriteLine("输入的月份不正确,日期有假!");
                    }
    
    
                }
                else
                {
                    Console.WriteLine("输入的年份不正确!日期有假");
                }
            }
    
    

    2、switch 语句

    如果说可选的条件比较多时,选择switch语句,要比if语句效率要高。特别注意的是 case 后跟的break。

    eg:

     1  //eg.6 swtich语句   作用域
     2         static void Maine(string[] args)
     3         {
     4             //Console.WriteLine("你本次选择出场的英雄是:");
     5             Random r = new Random();
     6             int n = r.Next(10);
     7 
     8             string a;
     9 
    10             switch (n)
    11             {
    12                 case 1: 
    13                     a = "赵信";    break;
    14                 case 2:
    15                     a = "寒冰射手";break;
    16                 case 3:
    17                     a = "无极剑圣";break;
    18                 case 4:
    19                     a = "机器人";  break;
    20                 default:
    21                     a = "齐天大圣";break;
    22             }
    23             Console.WriteLine("本次选择的英雄是:"+a);
    24         }

    三、循环语句

    for循环

    四要素:

    初始条件,循环条件,状态改变,循环体。 执行过程:

    初始条件--循环条件--循环体--状态改变--循环条件....

    注意:for的小括号里面分号隔开,for的小括号后不要加分号。

    利用 加断点的方式,可以更好的明白for的工作原理。

    1.for循环空操作完成的实例, 输出100以内的数字

     1 static void Main(string[] args)
     2         {
     3             int i = 1;
     4             for (; ; )
     5             {
     6                 if (i > 100)
     7                 {
     8                     break;
     9                 }
    10                 Console.Write(i + "	");
    11                 i++;
    12             }
    13             Console.ReadKey();
    14         }

    当然你也可以用 while,if() break;的嵌套完成上述操作。

    2.正序和逆序的推断问题。 (折纸问题)

     1  //eg.5 折纸问题
     2         static void Maine(string[] args)
     3         {
     4             //Console.WriteLine("请输入次数");
     5             //int n = Convert.ToInt32(Console.ReadLine());
     6 
     7 
     8             //int i = 0;
     9             //for (double sum = 0.0001; sum <= 8848.0; sum = sum * 2)
    10             //{
    11             //    i++;
    12 
    13             //}
    14             //Console.WriteLine(i);
    15 
    16             double sum = 0.0001;
    17             int z = 0;
    18 
    19             for (int i = 0; ; i++)
    20             {
    21                 z++;
    22                 sum = sum * 2;
    23 
    24                 if (sum >= 8848.0)
    25                 {
    26                     Console.WriteLine(z);
    27                     break;
    28                 }
    29             }
    30         }

    3.应用:a.穷举法: 用循环把各种可能的情况都给走一遍,然后用if条件把满足要求的结果给筛选出来。

      1 //eg.6 百马百石 大马驮2石,中马驮1石 小马驮0.5石 
      2         static void Main6a(string[] args)
      3         {
      4             for (int i = 0; i <= 50; i++)
      5             {
      6                 for (int j = 0; j <= 100; j++)
      7                 {
      8                     for (int k = 0; k <= 200; k++)
      9                     {
     10                         if ( (i * 2 + j * 1 + k * 0.5 == 100) && (i + j + k == 100) )
     11                         {
     12                             Thread.Sleep(50);
     13                             Console.WriteLine("大马需要" + i + "头,中马需要" + j + "头,小马需要" + k + "头。");
     14                         }
     15                     }
     16                 }
     17             }
     18         }
     19         //eg.7 
     20         static void Maing(string[] args)
     21         {
     22             for (int i = 1; i < 10; i++)
     23             {
     24                 for (int j = 1; j < 5; j++)
     25                 {
     26                     for (int k = 1; k < 25; k++)
     27                     {
     28                         if (i * 5 + j * 10 + k * 25 == 50)
     29                         {
     30                             Console.WriteLine("50元用来买" + i.ToString() + "个牙刷," + j.ToString() + "个牙膏," + k.ToString() + "块肥皂,正好能用完。");
     31                         }
     32                     }
     33                 }
     34             }
     35 
     36         }
     37         //eg.8 有1块,2块,5块的钱若干,凑出20块钱,有几种凑法
     38         static void Mainh(string[] args)
     39         {
     40             int m = 0;
     41             for (int i = 0; i <= 20; i++)
     42             {
     43                 for (int j = 0; j <= 10; j++)
     44                 {
     45                     for (int k = 0; k < 4; k++)
     46                     {
     47                         if (i * 1 + 2 * j + 5 * k == 20)
     48                         {
     49                             m++;
     50                             Console.WriteLine("一共有" + m + "中方法。");
     51                             Console.WriteLine("需要1元的" + i + "张,2元的" + j + "张,5元的" + k + "张。");
     52                         }
     53                     }
     54                 }
     55             }
     56         }
     57         //eg.9  1 () 2 () 3 ()4 = 4;问括号里我要填 (- 或 +)
     58         static void Maini(string[] args)
     59         {
     60             for (int i = 1; i <= 1; i += 2)
     61             {
     62                 for (int j = -1; j <= 1; j += 2)
     63                 {
     64                     for (int k = -1; k <= 1; k += 2)
     65                     {
     66                         for (int l = -1; l <= 1; l += 2)
     67                         {
     68                             if (1 * i + 2 * j + 3 * k + l * 4 == 4)
     69                             {
     70                                 Console.WriteLine("i=" + i + ",j=" + j + ",k=" + k + ",l=" + l + "");
     71                             }
     72                         }
     73 
     74 
     75                     }
     76                 }
     77             }
     78         }
     79         //eg.10  123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
     80         static void Maini2(string[] args)
     81         {
     82             for (int a = -1; a <= 2; a += 2)
     83             {
     84                 for (int b = -1; b <= 2; b += 2)
     85                 {
     86                     for (int c = -1; c <= 2; c += 2)
     87                     {
     88                         for (int d = -1; d <= 2; d += 2)
     89                         {
     90                             if (123 + a * 45 + b * 67 + c * 8 + d * 9 == 100)
     91                                 Console.WriteLine("a=" + a + ",b=" + b + ",c=" + c + ",d=" + d);
     92                         }
     93                     }
     94                 }
     95             }
     96             Console.ReadKey();
     97         }
     98         //eg.11 某侦查队接到一项紧急任务,要求在A.B.C,D,E,F六名队员中尽可能多的挑选若干人。A和B两人必须去一人。A和D不能同时去。A,E,F三人必须两人去。B和C都
    //去或都不去。C和D两人中去一人。若D不去,E也不去。问应叫哪几个人去?(灵活运用1与0)
     99         static void Mainj(string[] args)
    100         {
    101             for (int a = 0; a <= 1; a++)
    102             {
    103                 for (int b = 0; b <= 1; b++)
    104                 {
    105                     for (int c = 0; c <= 1; c++)
    106                     {
    107                         for (int d = 0; d <= 1; d++)
    108                         {
    109                             for (int e = 0; e <= 1; e++)
    110                             {
    111                                 for (int f = 0; f <= 1; f++)
    112                                 {
    113                                     if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && (d - e >= 0))
    114                                     {
    115                                         Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
    116                                     }
    117 
    118                                 }
    119                             }
    120                         }
    121                     }
    122                 }
    123             }
    124         }
    125         //老师版
    126         static void Mainj1(string[] args)
    127         {
    128             int a, b, c, d, e, f;
    129             for (a = 0; a < 2; a++)
    130             {
    131                 for (b = 0; b < 2; b++)
    132                 {
    133                     for (c = 0; c < 2; c++)
    134                     {
    135                         for (d = 0; d < 2; d++)
    136                         {
    137                             for (e = 0; e < 2; e++)
    138                             {
    139                                 for (f = 0; f < 2; f++)
    140                                 {
    141                                     if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && ((d + e == 0) || d == 1))
    142                                     {
    143                                         Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
    144                                     }
    145                                 }
    146                             }
    147                         }
    148                     }
    149                 }
    150             }
    151             Console.ReadKey();
    152         }

    b.迭代法:有一定规律。 每次循环都是从上次运算结果中获得数据,本次运算的结果都是要为下次运算做准备。

    eg1 兔生兔问题

    有一对幼兔,幼兔一个月后成长为小兔,小兔一个月后成长为成兔并生下一对幼兔,问几年后有多少对兔子,其中幼兔,小兔,成兔分别是多少?

     1 //eg.2 兔生兔问题
     2 
     3         //方法一
     4         static void Maink3(string[] args)
     5         {
     6             int syt = 1, byt = 0;
     7             int sxt = 0, bxt = 0;
     8             int sct = 0, bct = 0;
     9 
    10             Console.WriteLine("请输入月数:");
    11             int month = Convert.ToInt32(Console.ReadLine());
    12             int sum;
    13 
    14             for (int i = 1; i <= month; i++)
    15             {
    16                 //赋值顺序不能变,必须按照兔子生长规律来,先有的bct才会有byt
    17                 bct = sxt + sct;
    18                 bxt = syt;
    19                 byt = sxt + sct;
    20 
    21                 //bct = sxt + sct; 这样写,必须注意他的顺序
    22                 //bxt = syt;
    23                 //byt = bct;
    24 
    25 
    26 
    27                 //byt = bct;//错误的
    28                 //bxt = syt;
    29                 //bct = sxt + sct;  
    30 
    31                 syt = byt;
    32                 sxt = bxt;
    33                 sct = bct;
    34 
    35                 //sum = byt + bxt + bct;
    36             }
    37 
    38             sum = byt + bxt + bct;
    39 
    40             Console.WriteLine("过了{0}个月后,幼兔个数为{1}对,小兔个数为{2}对,成兔个数为{3}对,总共有{4}对。", month.ToString(), byt, bxt, bct,sum);
    41           
    42         }
    43         //方法二
    44         static void Maink4(string[] args)
    45         {
    46             int n = Convert.ToInt32(Console.ReadLine());
    47             int tu = 0;//要求那个月的总数
    48             int tu1=1, tu2=1;//倒数第一个为 tu1,倒数第二个为 tu2
    49 
    50             for (int i = 3; i < n;i++ )
    51             {
    52                 tu = tu1 + tu2;
    53                 tu2 = tu1;
    54                 tu1 = tu;
    55             }
    56             Console.WriteLine(tu);
    57 
    58         }
    59         //方法三
    60         static void Maink5(string[] args)
    61         {
    62             Console.Write("请输入月数:");
    63             int m = int.Parse(Console.ReadLine());
    64             int ct = 0;//成兔的对数
    65             int xt = 0;//小兔的对数
    66             int yt = 1;//
    67             int zt = 1;//
    68 
    69             for (int i = 1; i <= m; i++)
    70             {
    71                 if (i == 1)
    72                 {
    73                     ct = 0;
    74                     xt = 0;
    75                     yt = 1;
    76                 }
    77                 else
    78                 {
    79                     ct = xt + ct;
    80                     xt = yt;
    81                     yt = ct;
    82                 }
    83                 zt = yt + xt + ct;
    84                 
    85                 Console.WriteLine(i.ToString() + "个月后成兔的对数是:" + ct.ToString());
    86                 Console.WriteLine(i.ToString() + "个月后小兔的对数是:" + xt.ToString());
    87                 Console.WriteLine(i.ToString() + "个月后幼兔的对数是:" + yt.ToString());
    88                 Console.WriteLine(i.ToString() + "个月后兔子的总对数是:" + zt.ToString());
    89             }
    90             Console.ReadLine();
    91         }

    eg 2  100以内的所有数的和。

    eg3. 求阶乘。
    eg4.求年龄。
    eg5.折纸。
    eg6.棋盘放粮食。
    eg7.猴子吃桃子。

     1   static void Maink(string[] args)
     2         {
     3             int sum = 1;
     4             for (int i = 0; i < 6; i++)
     5             {
     6                 int t = (sum + 1) * 2;
     7                 sum = t;
     8             }
     9             Console.WriteLine("桃子一共有:" + sum + "个。");
    10         }


    eg8.落球问题。一个球从10米高度落下,每次弹起2/3的高度,问第五次弹起后的高度?

    eg.9

    打印菱形

     1 //eg.2 打印菱形(教材版)
     2         static void Mainb(string[] args)
     3         {
     4             //菱形上半部
     5             Console.WriteLine("请输入一个数:");
     6             int n = int.Parse(Console.ReadLine());
     7             for (int i = 1; i <= n; i++)
     8             {
     9                 for (int b = 1; b <= n - i; b++)
    10                 {
    11                     Console.Write("  ");
    12                 }
    13                 for (int j = 1; j <= 2 * i - 1; j++)
    14                 {
    15                     Console.Write("");
    16                 }
    17                 Console.Write("
    ");
    18             }
    19 
    20             //菱形下半部
    21             for (int p = 1; p < n; p++)
    22             {
    23                 for (int o = 1; o <= p; o++)
    24                 {
    25                     Console.Write("  ");
    26                 }
    27                 for (int k = 1; k <= 2 * (n - p) - 1; k++)
    28                 {
    29                     Console.Write("");
    30                 }
    31                 Console.Write("
    ");
    32             }
    33             Console.ReadKey();
    34         }
    35 
    36  //eg.4 打印菱形(自己做)
    37         static void Maind(string[] args)
    38         {
    39             Console.WriteLine("请输入要打印菱形的中间层个数:");
    40             int a = Convert.ToInt32(Console.ReadLine());
    41 
    42             //组成菱形必须是奇数行
    43             if (a % 2 != 0)
    44             {
    45                 int c = (a + 1) / 2;
    46                 //上半部
    47                 for (int i = 1; i <= c; i++)
    48                 {
    49                     for (int j = i; j <= c - 1; j++)
    50                     {
    51                         Console.Write("  ");
    52                     }
    53                     for (int k = 1; k <= 2 * i - 1; k++)
    54                     {
    55                         Console.Write("");
    56                     }
    57                     Console.WriteLine();
    58                 }
    59 
    60                 //下半部
    61                 for (int m = c + 1; m <= a; m++)
    62                 {
    63                     for (int n = 1; n <= m - c; n++)
    64                     {
    65                         Console.Write("  ");
    66                     }
    67                     for (int o = 2 * (a - m) + 1; o >= 1; o--)
    68                     {
    69                         Console.Write("");
    70                     }
    71                     Console.WriteLine();
    72                 }
    73             }
    74             else
    75             {
    76                 Console.WriteLine("你所输入的层数不可能组成菱形。");
    77             }
    78 
    79         }

    四、while 循环。一般用在一些死循环当中。

    五、try catch。保护程序,避免程序出错时无法运行。

    格式:

     try//快捷方式:双击 tab键                                                                        
                {
    
                }
                catch (Exception)
                {
    
                    throw;
                }
                finally
                {
     
                }

     六、跳转语句

    1.break。跳出循环体。

    eg

    如果循环有多层嵌套,停止的是最贴近break的那个循环,即循环2
    for()循环1
    {
    for()循环2
    {
    if()
    {
    break
    }
    }
    }

    2.continue。跳出本次循环,进行下一次循环。

     

  • 相关阅读:
    收集的正则表达式
    全面解析JavaScript中“&&”和“||”操作符(总结篇)
    3.5 二叉查找树的几何应用
    3.4 散列表
    3.3 平衡查找树
    3.2 符号表之二叉查找树BST
    3.1 符号表之二分查找
    2.7 二叉堆及优先队列
    2.6 经典排序算法总结
    2.5 3-way quickSort
  • 原文地址:https://www.cnblogs.com/zyh-club/p/4621254.html
Copyright © 2011-2022 走看看