For 循环嵌套 0309

妖精的绣舞 提交于 2020-03-27 03:39:16

                                                                                     

                                                                                                 For 循环嵌套0309

  

 

循环四要素:初始条件,循环条件,循环体,状态改变。

for(初始条件;循环条件;状态改变)

{

      循环体

}

break ——中断循环,跳出循环

continue——停止本次循环,进入下次循环

 

 

  1. 输入一个整数,计算从1加到这个数的结果

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

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

            int sum = 0;//0加任何数不变

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

            {

                sum += i;

            }

            Console.WriteLine(sum);

 

 

2、输入一个正整数n,求1-n的阶乘

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

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

            int sum = 1;//1乘任何数不变

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

            {

                sum *= i;

            }

            Console.WriteLine("阶乘为"+sum);

 

3、.案例:找出100以内与7有关的数并打印、

(1).从1找到100

(2).找出与7有关的数

      个位数是7 a%10==7

      十位数是7 a/10 ==7

      能被7整除 a%7 ==0

            int a = 100;

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

            {

                if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)

                {

                    Console.WriteLine(i);

                }

 

            }

4.一个游戏,前20关是每一关自身的分数,

            21-30关每一关是10分

            31-40关,每一关是20分

            41-49关,每一关是30分

            50关,是100分

            输入你现在闯到的关卡数,求你现在拥有的分数

if嵌套for   Console.Write("输入关卡数:");

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

            int sum = 0;

            if (a >= 1 && a <= 50)

            {

                if (a >= 1 && a <= 20)

                {

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

                    {

                        sum = sum + i;

                    }

                }

                else if (a >= 21 && a <= 30)//21-30

                {

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

                    {

                        sum = sum + i;

                    }

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

                    {

                        sum = sum + 10;

                    }

                }

                else if (a >= 31 && a <= 40)//31-40

                {

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

                    {

                        sum = sum + i;

                    }

                    for (int i = 21; i <= 30; i++)

                    {

                        sum = sum + 10;

                    }

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

                    {

                        sum = sum + 20;

                    }

                }

                else if (a >= 41 && a <= 49)//41-49

                {

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

                    {

                        sum = sum + i;

                    }

                    for (int i = 21; i <= 30; i++)

                    {

                        sum = sum + 10;

                    }

                    for (int i = 31; i <= 40; i++)

                    {

                        sum = sum + 20;

                    }

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

                    {

                        sum = sum + 30;

                    }

                }

                else//50

                {

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

                    {

                        sum = sum + i;

                    }

                    for (int i = 21; i <= 30; i++)

                    {

                        sum = sum + 10;

                    }

                    for (int i = 31; i <= 40; i++)

                    {

                        sum = sum + 20;

                    }

                    for (int i = 41; i <= 49; i++)

                    {

                        sum = sum + 30;

                    }

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

                    {

                        sum = sum + 100;

                    }

                }

            }

            else

            {

                Console.WriteLine("你输入的关卡有误!");

            }

            Console.WriteLine(sum);

 

 

for嵌套if  Console.Write("输入关卡数:");

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

            int sum = 0;

            if (a >= 1 && a <= 50)

            {

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

                {

                    if (i >= 1 && i <= 20)

                    {

                        sum = sum + i;

                    }

                    if (i >= 21 && i <= 30)

                    {

                        sum = sum + 10;

                    }

                    if (i >= 31 && i <= 40)

                    {

                        sum = sum + 20;

                    }

                    if (i >= 41 && i <= 49)

                    {

                        sum = sum + 30;

                    }

                    if (i == 50)

                    {

                        sum = sum + 100;

                    }

                }

                Console.WriteLine("总得分:" + sum);

            }

            else

            {

                Console.WriteLine("你输入的关卡数有误!");

            }

 

 

 

5.  100节楼梯,0-49节的每一节是本身的分数,

    从50开始是每一关10分。

输入现在所到的节数,求现在的分数

            Console.Write("请输入楼梯节数:");

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

            int sum = 0;

            if (a >= 1 && a <= 100)

            {

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

                {

                    if (i >= 1 && i <= 49)

                    {

                        sum = sum + i;

                    }

                    else//(i>=50&&i<=100)

                    {

                        sum = sum + 10;

                    }

                }

                Console.WriteLine("得分:" + sum);

            }

            else

            {

                Console.WriteLine("你输入的楼梯节数超过范围。");

            }

 

 

 

6、打印三角,四种情况

 //五角/1.0版(左下角直角)

            int a = 5;

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

            {

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

                {

                    Console.Write("☆");

                }

                Console.WriteLine();

            }

 

 

//五角2.0版(左上角直角)

            int a = 5;

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

            {

                for (int j = 5; j >= i; j--)

                {

                    Console.Write("☆");

                }

                Console.WriteLine();

            }

 

 

//五角星3.0版(右下角直角)

            int a = 5;

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

            {

                for (int j = a - 1; j >= i; j--)

                {

                    Console.Write("  ");

                }

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

                {

                    Console.Write("☆");

                }

                Console.WriteLine();

            }

 

 

 

//五角/4.0版(右上角直角)

            int a = 5;

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

            {

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

                {

                    Console.Write("  ");

                }

                for (int k = a; k >= i; k--)

                {

                    Console.Write("☆");

                }

                Console.WriteLine();

            }

 

 

 

 

 

7、输入一个整数,求1!+2!+...+n!

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

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

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

            int sum = 0;

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

            {

                int sum1 = 1;

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

                {

                    sum1 = sum1 * j;

                }

                sum = sum + sum1;

            }

            Console.Write("阶乘的和:" + sum);

8、//九九乘法表

            int a = 9;

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

            {

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

                {

                    Console.Write(j + "*" + i + "=" + (i * j) + "\t");

                }

                Console.WriteLine();

            }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!