• 语句 (for break)


    for 循环,循环执行,循环完成
                
                 for(初始条件,循环执行,状态改变)
                 {
                   循环体,执行代码;
                 }        

     
    例如,    for (int i = 1; i < 10; i = ++i)
                {
                    Console.WriteLine(i);
                     if (i == 7)
                    { break; }                 //满足if条件,此时break可以直接跳出for循环!
                }
                Console.ReadLine(); */


                //输入正整数n,求出1到n的和      
             
                Console.WriteLine ("请输入一个正整数n:");
                int n=int .Parse (Console.ReadLine ());
                int a=0;
                for (int i = 1; i <= n; i = ++i)
                { a+=i;    }                   //表示累加到   n的总和
                Console.WriteLine("1到"+n+"的和:" + a);
                Console.ReadLine(); 

        //100节楼梯,前50节每节的分值对应它的节数,后50节每节10分,输入节数,得出分数
               
                Console.WriteLine("输入数值0~100:");
                int a=int.Parse (Console.ReadLine());
                int c = 0; 
                for (int i = 0; i<= a; i++) 
                {
                    if (i<= 50)
                    {c+=i; }       //表示累加到50的和
                    else
                    {c+= 10;}   //后50节每节10分,此时表示    c 之前50层总和再加   +10(每一层运算一次for循环)
                      
                       //for循环一直运算,直到i=a为止,输出c值(包括if全部情况)!
                }
              Console.Write("总得分:"+c);
              Console.ReadLine();

    //

                        用☆打印等边三角形!

     int chang = int.Parse(Console.ReadLine());
                for (int i = 0; i < chang; i++)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        Console.Write("☆");    //打印  i 个  “☆”
                    }
                    Console.Write(" ");       //打印完了,换行!  
                }
                Console.ReadLine();

    //用for循环打印菱形!
                    for (int c = 1; c <=9; c++)
                    {
                        for (int d = 9; d >=c; d--)
                        {
                            Console.Write(" ");
                        }                                        //先打印   “空格”
                        for (int f = 1; f <= (2*c-1); f++)
                        {
                            Console.Write("#");
                        }
                            Console.Write(" ");
                    }              //打印上三角形。
                    for (int c = 8; c >= 1; c--)
                    {
                        for (int d = 0; d <=(9- c); d++)
                        {
                            Console.Write(" ");
                        }
                        for (int f =1 ; f <=(2*c- 1); f++)
                        {
                            Console.Write("#");
                        }
                        Console.Write(" ");
                    }               //打印下三角形。   
                Console.ReadLine();


                
           

       /*n的阶乘*/            

                Console.Write("输入一个整数值N:");

                int a = int.Parse(Console.ReadLine());

                int d = 1;

                for (int c = 1; c <= a; c++)      //当满足c<=a时,进入for循环运算;不满足时,终止!

                {                                  

                           d = d*c;         //等同于d*=c。        例如,1*2*3*4*5*……

                 }

                     Console.Write ("N的阶乘:"+d);

                    Console.ReadLine ();

           //假设现在有幼兔5对,小兔2对,成兔3对,兔子每隔3个月升一级,成兔每隔3个月生一对幼兔。求N个月之后有多少对兔子?       

                     Console.Write("输入月份数:");

                     int month=Convert.ToInt16 (Console.ReadLine ());

                     int y = 0;

                     int x = 0;

                     int c = 0;

                     int Z = 0;

                     for (int i = 0; i <= month / 3; i++)

                     {

                         if (i == 1)                             //第一个月输入初始值!

                        {

                            y = 5;

                             x = 2;

                             c = 3;

                        }

                         else

                         {

                            c = c + x;

                             x = y;

                             y = c;

                             //此处运算顺序不能颠倒,这里是赋值上个“月”,得出这个“月”该变量值。顺序颠倒就错了!

                        }

                      Z=c+x+y;

                    }

                 Console.WriteLine("兔子总共:" + Z.ToString()); //Z.ToString()   : 一定记得符号()

                    Console.ReadLine();

  • 相关阅读:
    基于antd封装一个高可用form组件 减少cv代码导致的bug
    写一个umi插件 自动生成代码 解放cv的双手
    jquery.validate的一个bug
    ef6+mysql的bug
    angularJS使用ocLazyLoad实现js延迟加载
    postgresql,postgis,geoserver 发布地图服务,并用.net mvc openlayers3进行显示
    EF框架操作postgresql,实现WKT类型坐标的插入,查询,以及判断是否相交
    jquery validate 之多tab页同时校验问题
    js去除空格12种方法
    个人工具清单
  • 原文地址:https://www.cnblogs.com/huaze/p/4024790.html
Copyright © 2020-2023  润新知