• 2017-2-24 C#基础 跳转语句 迭代法 穷举法 异常语句 while 循环


    1、跳转语句:
    break - 跳出循环
    continue - 停止当前次循环,继续下一次循环

    2、

    迭代法:
    不断在自身上增加新的功能

    namespace _2017_2_24_迭代法__练习
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 0;
                    for(int i=1;i<=10;i++)
                    {
                        a++;
                    }
                Console.WriteLine(a);
                Console.ReadLine ();
            }
        }
    }

    3、穷举法:就是把所有的可能都一一列举出来;代表题目 :百鸡百钱 , 有100文钱,小鸡0.5文钱 ,母鸡1文钱,公鸡2文钱。

    namespace _2017_2_24___穷举法__百鸡百钱
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 0;
                for (int x = 0; x <= 200;x++ ) 
                {
                    for (int m = 0; m <= 100; m++) 
                    {
                        for (int g = 0; g <= 50; g++)
                        {
                            if ((x * 0.5) + (m * 1) + (g * 2) == 100&&x+m+g==100) 
                            {
                                Console.WriteLine("小鸡"+x+"只,花费"+(x*0.5)+"元;母鸡"+m+"只,花费"+m+"元;公鸡"+g+"只,花费"+(g*2)+"元。");
                      a++; } } }
    } Console.WriteLine("一共"+a+"种可能。"); Console.ReadLine(); } } }

    4、异常语句:
    try
    {
    可能会出错的代码语句
    如果这里出错了,那么不会在继续下面的代码,而是直接进入catch中处理异常
    }
    catch
    {
    如果上面出错了,这里是对这个异常的处理方式;
    }
    finally//可写可不写
    {
    不管上面有没有错,这里都会走,
    }

    namespace _2017_2_24_异常语句
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入一个数字:");
                String a=Console.ReadLine();
                try 
                {
                    int b = Convert.ToInt32(a);
                    Console.WriteLine("很好,输入的是数字!");
                }
                catch
                {
                    Console.WriteLine("输入的不是数字");
                }
                finally 
                {
                    Console.WriteLine("这是最后一句话");
                }
                Console.ReadLine();
            }
        }
    }

    5、while循环:

    namespace _2017_2_24___while_循环
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a = 0; 
                int b = 1;//初始条件
                while (b <= 10)//循环条件
                {
                    a++;//循环体
                    b++;//状态改变
                }
                Console.WriteLine(a);
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    代码点与代码单元
    IIS最大并发连接数
    PhoneGap:JS跨域请求
    字符串长度
    android学习笔记:adb更换端口后成功启动
    java学习笔记:eclipse的workspace和working set
    java学习笔记:Eclipse打开现有项目
    java学习笔记:文件名区分大小写
    mysql学习笔记:存储过程
    mySql学习笔记:比sql server书写要简单
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6472644.html
Copyright © 2020-2023  润新知