首页 > 代码库 > 语句 (for break)

语句 (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节每节1分,后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; }       //表示累加到a的和
                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("\n");       //打印完了,换行!  
            }
            Console.ReadLine();


            
       

   /*n的阶乘*/              问题程序!

            Console.Write("输入一个整数值N:");             int a = int.Parse(Console.ReadLine());             int d = 0;             for (int c = 0; c <= a; c++)             {                                             d = d*d;                }                 Console.Write ("N的阶乘:"+d);                 Console.ReadLine ();

 

语句 (for break)