zoukankan      html  css  js  c++  java
  • 0505.Net基础班第五天(流程控制)

    1、*程序调试 1)、写完一段程序后,想看一下这段程序的执行过程。 2)、当你写完这段程序后,发现,程序并没有按照你想象的样子去执行。

    调试方法: 1)、F11逐语句调试(单步调试) 2)、F10逐过程调试 3)、断点调试

    2、for循环 语法: for(表达式1;表达式2;表达式3) {  循环体; } 表达式1一般为声明循环变量,记录循环的次数(int i=0;) 表达式2一般为循环条件(i<10) 表达式3一般为改变循环条件的代码,使循环条件终有一天不再成立(i++)。 执行过程:程序首先执行表达式1,声明了一个循环变量用来记录循环的次数, 然后执行表达式2,判断循环条件是否成立,如果表达式2返回的结果为true, 则执行循环体。当执行完循环体后,执行表达式3,然后执行表达式2继续判断循环条件是否成立, 如果成立则继续执行循环体,如果不成立,则跳出for循环。

    3、int.TryParse int.parse 尝试着将一个字符串转换成int类型。

    4、三元表达式 语法: 表达式1?表达式2:表达式3; 表达式1一般为一个关系表达式。 如果表达式1的值为true,那么表达式2的值就是整个三元表达式的值。 如果表达式1的值为false,那么表达式3的值就是整个三元表达式的值。 注意:表达式2的结果类型必须跟表达式3的结果类型一致,并且也要跟整个三元表达式的结果类型一致。

    01复习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _01复习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //练习4:不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出。
    14             //循环体:提示用户输入一个数字 接收 转换  打印2倍
    15             //循环条件:输入的不能是q
    16 
    17             //string input = "";
    18             //while (input != "q")
    19             //{
    20             //    Console.WriteLine("请输入一个数字,我们将打印这个数字的2倍");
    21             //    //不能直接转换成int类型 因为用户有可能输入q
    22             //    input = Console.ReadLine();//数字 q 乱七八糟
    23             //    if (input != "q")
    24             //    {
    25             //        try
    26             //        {
    27             //            int number = Convert.ToInt32(input);
    28             //            Console.WriteLine("您输入的数字的2倍是{0}", number * 2);
    29             //        }
    30             //        catch
    31             //        {
    32             //            Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
    33             //        }
    34             //    }
    35             //    else//==q
    36             //    {
    37             //        Console.WriteLine("输入的是q,程序退出");
    38             //    }
    39             //}
    40 
    41             //练习5:不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候显示刚才输入的数字中的最大值
    42             //循环体:提示用户输入一个数字  接收  转换成int类型  不停的比较大小
    43             //循环条件:输入的不能是end
    44             //F11
    45             string input = "";
    46             int max = 0;
    47             while (input != "end")
    48             {
    49                 Console.WriteLine("请输入一个数字,输入end我们将显示你输入的最大值");
    50                 input = Console.ReadLine();//数字 end  乱七八糟
    51                 if (input != "end")
    52                 {
    53                     try
    54                     {
    55                         int number = Convert.ToInt32(input);
    56                         //让用户输入的每个数字都跟我假定的最大值比较,只要比我假定的最大值要大,
    57                         //就把当前输入的这个数字赋值给我的最大值
    58                         if (number > max)
    59                         {
    60                             max = number;
    61                         }
    62                     }
    63                     catch
    64                     {
    65                         Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
    66                     }
    67                    
    68                 }
    69                 else//==end
    70                 {
    71                     Console.WriteLine("您刚才输的数字中的最大值是{0}",max);
    72                 }
    73             }
    74             Console.ReadKey();
    75 
    76 
    77             Console.ReadKey();
    78         }
    79     }
    80 }
    View Code

    02调试

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _02调试
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("Hello World");
    14             Console.WriteLine("Hello World");
    15             Console.WriteLine("Hello World");
    16             Console.WriteLine("Hello World");
    17             Console.WriteLine("Hello World");
    18             Console.WriteLine("Hello World");
    19             Console.WriteLine("Hello World");
    20 
    21             //程序运行断点处,就不在向下执行了
    22             Console.WriteLine("这行代码有可能有错误");
    23             Console.WriteLine("这行代码有可能有错误");
    24             Console.WriteLine("这行代码有可能有错误");
    25             Console.WriteLine("这行代码有可能有错误");
    26             Console.WriteLine("这行代码有可能有错误");
    27             Console.WriteLine("Hello World");
    28             Console.WriteLine("Hello World");
    29             Console.WriteLine("Hello World");
    30             Console.WriteLine("Hello World");
    31             Console.WriteLine("Hello World");
    32             Console.WriteLine("Hello World");
    33             Console.WriteLine("Hello World");
    34             Console.WriteLine("Hello World");
    35         }
    36     }
    37 }
    View Code

    03for循环

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _03for循环
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //向控制台打印10遍  欢迎来到传智播客.Net学习
    14 
    15             for (int i = 0; i < 10; i++)
    16             {
    17                 Console.WriteLine("欢迎来到传智播客.Net学习{0}", i);
    18             }
    19             //for (int i = 0; i < length; i++)
    20             //{
    21                 
    22             //}
    23             Console.ReadKey();
    24 
    25             //int i = 0;//定义循环的次数
    26             //while (i < 10)
    27             //{
    28             //    Console.WriteLine("欢迎来到传智播客.Net学习");
    29             //    i++;
    30             //}
    31             //Console.ReadKey();
    32         }
    33     }
    34 }
    View Code

    04for循环的正序输出和倒序输出

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _04for循环的正序输出和倒序输出
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //请打印 1-10
    14             int i = 0;
    15             for (; i < 10; )
    16             {
    17                 Console.WriteLine("欢迎来到传智播客.Net学习");
    18                 i++;
    19             }
    20             Console.ReadKey();
    21 
    22             //for (int i = 1; i <= 10; i++)
    23             //{
    24             //    Console.WriteLine(i);
    25             //}
    26             ////打印10-1
    27             //for (int i = 10; i >= 1; i--)
    28             //{
    29             //    Console.WriteLine(i);
    30             //}
    31 
    32             //for (int i = 10; i >= 1; i--)
    33             //{
    34             //    Console.WriteLine(i);
    35             //}
    36             Console.ReadKey();
    37         }
    38     }
    39 }
    View Code

    05for循环的练习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _05for循环的练习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //求1-100之间的所有整数和   偶数和  奇数和
    14             //int sum = 0;
    15             //int n = 100;
    16             //for (int i = 1; i <= n; i += 2)
    17             //{
    18             //    sum += i;
    19             //}
    20             //Console.WriteLine(sum);
    21             //Console.ReadKey();
    22 
    23             //找出100-999间的水仙花数?
    24             //水仙花数指的就是 这个百位数字的
    25             //百位的立方+十位的立方+个位的立方==当前这个百位数字
    26             //153  1 125  27  153  i
    27             //百位:153/100
    28             //十位:153%100/10
    29             //个位:153%10
    30 
    31             for (int i = 100; i <= 999; i++)
    32             {
    33                 int bai = i / 100;
    34                 int shi = i % 100 / 10;
    35                 int ge = i % 10;
    36                 if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
    37                 {
    38                     Console.WriteLine("水仙花数有{0}",i);
    39                 }
    40             }
    41             Console.ReadKey();
    42 
    43         }
    44     }
    45 }
    View Code

    06for循环的嵌套

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _06for循环的嵌套
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             
    14 
    15             //当遇到某个事情要做一遍,而另外一个事情要做N遍的时候
    16             //for循环的嵌套
    17             for (int i = 0; i < 10; i++)
    18             {
    19                 for (int j = 0; j < 10; j++)
    20                 {
    21                     Console.WriteLine("Hello World i循环了{0}次,j循环了{1}次",i,j);
    22                     break;
    23                 }
    24             }
    25             Console.ReadKey();
    26         }
    27     }
    28 }
    View Code

    07乘法口诀表

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _07乘法口诀表
     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}*{1}={2}	", i, j, i * j);
    18             //    }
    19             //    Console.WriteLine();//换行
    20             //}
    21             //Console.ReadKey();
    22 
    23             //Console.Write("Hello Wor	ld");
    24             //Console.WriteLine();
    25             //Console.Write("Hello World");
    26             //Console.ReadKey();
    27 
    28             Console.WriteLine("请输入一个数字");
    29             int number = Convert.ToInt32(Console.ReadLine());
    30 
    31 
    32             for (int i = 0; i <=number; i++)
    33             {
    34                 Console.WriteLine("{0}+{1}={2}",i,number-i,number);
    35             }
    36             Console.ReadKey();
    37         }
    38     }
    39 }
    View Code

    08类型转换

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _08类型转换
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //使用Convert进行转换 成功了就成了, 失败了就抛异常
    14             // int numberOne = Convert.ToInt32("123abc");
    15 
    16             // int number = int.Parse("123abc");
    17 
    18             //Console.WriteLine(number);
    19             int number = 100;
    20             //参数 返回值
    21             bool b = int.TryParse("123abc", out number);
    22             Console.WriteLine(b);
    23             Console.WriteLine(number);
    24             //方法 或者 函数?
    25             Console.ReadKey();
    26         }
    27     }
    28 }
    View Code

    09for循环的3个练习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _09for循环的3个练习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //练习1:循环录入5个人的年龄并计算平均年龄,
    14             //如果录入的数据出现负数或大于100的数,立即停止输入并报错.
    15             //int sum = 0;
    16             //bool b = true;
    17             //for (int i = 0; i < 5; i++)
    18             //{
    19             //    Console.WriteLine("请输入第{0}个人的成绩",i+1);
    20             //    try
    21             //    {
    22             //        int age = Convert.ToInt32(Console.ReadLine());
    23             //        if (age >= 0 && age <= 100)
    24             //        {
    25             //            sum += age;
    26             //        }
    27             //        else
    28             //        {
    29             //            Console.WriteLine("输入的年龄不在正确范围内,程序退出!!!");
    30             //            b = false;
    31             //            break;
    32             //        }
    33             //    }
    34             //    catch
    35             //    {
    36             //        Console.WriteLine("输入的年龄不正确,程序退出!!!");
    37             //        b = false;
    38             //        break;
    39             //    }
    40             //}
    41             //if (b)
    42             //{
    43             //    Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
    44             //}
    45             //Console.ReadKey();
    46 
    47 
    48        //     练习2:在while中用break实现要求用户一直输入用户名和密码,
    49             //只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
    50             //string name = "";
    51             //string pwd = "";
    52             //while (true)
    53             //{
    54             //    Console.WriteLine("请输入用户名");
    55             //    name = Console.ReadLine();
    56             //    Console.WriteLine("请输入密码");
    57             //    pwd = Console.ReadLine();
    58 
    59             //    if (name == "admin" && pwd == "888888")
    60             //    {
    61             //        Console.WriteLine("登陆成功");
    62             //        break;
    63             //    }
    64             //    else
    65             //    {
    66             //        Console.WriteLine("用户名或密码错误,请重新输入");
    67             //    }
    68             //}
    69             //Console.ReadKey();
    70 
    71 
    72             //1~100之间的整数相加,得到累加值大于20的当前数
    73             //(比如:1+2+3+4+5+6=21)结果6 sum>=20  i
    74             int sum = 0;
    75             for (int i = 1; i <= 100; i++)
    76             {
    77                 sum += i;
    78                 if (sum >= 20)
    79                 {
    80                     Console.WriteLine("加到{0}的时候,总和大于了20",i);
    81                     break;
    82                 }
    83             }
    84             Console.ReadKey();
    85 
    86 
    87         }
    88     }
    89 }
    View Code

    10continue

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _10continue
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             while (true)
    14             {
    15                 Console.WriteLine("Hello World");
    16                // break;
    17                 continue;
    18                 Console.WriteLine("Hello World");
    19                 Console.WriteLine("Hello World");
    20             }
    21             Console.ReadKey();
    22         }
    23     }
    24 }
    View Code

    11continue练习

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _11continue练习
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //练习1:用 while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和。
    14 
    15             //int sum = 0;
    16             //int i=1;
    17             //while (i <= 100)
    18             //{
    19             //    if (i % 7 == 0)
    20             //    {
    21             //        i++;
    22             //        continue;
    23             //    }
    24             //    sum += i;
    25             //    i++;
    26             //}
    27             //Console.WriteLine(sum);
    28             //Console.ReadKey();
    29 
    30 
    31             //找出100内所有的素数
    32             //素数/质数:只能被1和这个数字本身整除的数字
    33             //2 3  4  5  6  7
    34             //7   7%1   7%2 7%3 7%4 7%5 7%6  7%7  6%2
    35            
    36             for (int i = 2; i <= 100; i++)
    37             {
    38                 bool b = true;
    39                 for (int j = 2; j <i; j++)
    40                 {
    41                     //除尽了说明不是质数 也就没有再往下继续取余的必要了
    42                     if (i % j == 0)
    43                     {
    44                         b = false;
    45                         break;
    46                     }
    47                 }
    48 
    49                 if (b)
    50                 {
    51                     Console.WriteLine(i);
    52                 }
    53             }
    54 
    55             Console.ReadKey();
    56             //6   6%2 6%3
    57         }
    58     }
    59 }
    View Code

    12三元表达式

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _12三元表达式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             ////计算两个数字的大小 求出最大的
    14             //Console.WriteLine("请输入第一个数字");
    15             //int n1 = Convert.ToInt32(Console.ReadLine());
    16             //Console.WriteLine("请输入第二个数字");
    17             //int n2 = Convert.ToInt32(Console.ReadLine());
    18             ////            语法:
    19             ////表达式1?表达式2 :表达式3
    20             //int max = n1 > n2 ? n1 : n2;
    21             //Console.WriteLine(max);
    22             ////if (n1 > n2)
    23             ////{
    24             ////    Console.WriteLine(n1);
    25             ////}
    26             ////else
    27             ////{
    28             ////    Console.WriteLine(n2);
    29             ////}
    30             //Console.ReadKey();
    31 
    32 
    33             //提示用户输入一个姓名 只要输入的不是老赵  就全是流氓
    34             Console.WriteLine("请输入姓名");
    35             string name = Console.ReadLine();
    36 
    37             string result = name == "老赵" ? "淫才呀" : "流氓呀";
    38             Console.WriteLine(result);
    39             Console.ReadKey();
    40 
    41             //if (name == "老赵")
    42             //{
    43             //    Console.WriteLine("淫才呀");
    44             //}
    45             //else
    46             //{
    47             //    Console.WriteLine("流氓呀");
    48             //}
    49             Console.ReadKey();
    50 
    51 
    52         }
    53     }
    54 }
    View Code

    13、随机数

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _13_随机数
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //产生随机数
    14             //1、创建能够产生随机数的对象
    15             //Random r = new Random();
    16             //while (true)
    17             //{
    18 
    19             //    //2、让产生随机数的这个对象调用方法来产生随机数
    20             //    int rNumber = r.Next(1, 11);
    21             //    Console.WriteLine(rNumber);
    22             //    Console.ReadKey();
    23             //}
    24 
    25             //输入名字随机显示这个人上辈是什么样的人
    26             //思路:
    27             //1、创建能够产生随机数的对象
    28             //2、产生随机数 (1,7)
    29 
    30             Random r = new Random();
    31             while (true)
    32             {
    33                 int rNumber = r.Next(1, 7);
    34                 Console.WriteLine("请输入一个姓名");
    35                 string name = Console.ReadLine();
    36                 switch (rNumber)
    37                 {
    38                     case 1: Console.WriteLine("{0}上辈子是吃翔的", name);
    39                         break;
    40                     case 2: Console.WriteLine("{0}上辈子是拉翔的", name);
    41                         break;
    42                     case 3: Console.WriteLine("{0}上辈子就是一坨翔", name);
    43                         break;
    44                     case 4: Console.WriteLine("{0}上辈子是大汉奸", name);
    45                         break;
    46                     case 5: Console.WriteLine("{0}上辈子是拉皮条的", name);
    47                         break;
    48                     case 6: Console.WriteLine("{0}上辈子是救苦救难的活菩萨", name);
    49                         break;
    50                 }
    51                 Console.ReadKey();
    52             }
    53            
    54         }
    55     }
    56 }
    View Code
  • 相关阅读:
    面试中遇到递归算法题别慌--常见递归算法题的解题思路
    Xml日志记录文件最优方案(附源代码)
    linux下源码安装软件
    文本比较算法Ⅴ——回顾贴,对前面几篇文章的回顾与质疑
    从内存中直接运行PE程序
    谈谈Linux内核驱动的coding style
    【全面解禁!真正的Expression Blend实战开发技巧】第六章 认识ListBox
    玩转C链表
    麻省理工《C内存管理和C++面向对象编程》笔记---第一讲:认识C和内存管理
    Dll注入技术之输入法注入
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4713334.html
Copyright © 2011-2022 走看看