for循环

六、for循环

(一)格式

for(初始条件;循环条件;循环调整)

{

  循环体;

}

(二)例题

1、累加

主要代码:

while(true)

{

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

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

    double s = 0;

    for (int i = 1; i <= a; i++)

    {

        s += i;

    }

    Console.WriteLine(s);

}

Console.ReadLine();

结果:

2、阶乘

主要代码:

while(true)

{

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

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

    double s = 1;

    for (int i = 1; i <= a; i++)

    {

        s *= i;

    }

    Console.WriteLine(s);

 }

 Console.ReadLine();

结果:

原文地址:https://www.cnblogs.com/bosamvs/p/5451141.html