C#语句二——循环语句(for循环与for循环嵌套)

Posted 这都不是事

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#语句二——循环语句(for循环与for循环嵌套)相关的知识,希望对你有一定的参考价值。

循环:反复执行某段代码。

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

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

{

         循环体

}

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

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

注:●执行步骤:初始条件——循环条件——循环体——状态改变。

●死循环:出不来,一直在执行

for (; ; )

{

   Console.WriteLine("你好");

}

一、for循环例题

1、找出100以内与7有关的数并打印:(1).从1找到100(2).找出与7有关的数

●个位数是7 a%10==7

●十位数是7 a/10 ==7

●能被7整除 a%7 ==0

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

            {

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

                {

                    Console.Write(i+"\t");

                }

            }

            Console.ReadLine();

2、输入一个正整数,求1~这个数的阶乘

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

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

            int b=1;

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

            {

                b = b * n;

            }

            Console.WriteLine("这个数字的乘积是:"+b);

            Console.ReadLine();

3、一个游戏,前20关是每一关自身的分数,,21-30关每一关是10分,31-40关,每一关是20分,41-49关,每一关是30分,50关,是100分,输入你现在闯到的关卡数,求你现在拥有的分数。两种做法:if嵌套for    for嵌套if

法一:

            Console.Write("请输入您现在闯到的关卡数:");

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

            int sum = 0;

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

            {

                if (a <= 20)

                {

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

                    {

                        sum += i;

                    }

                }

                else if (a <= 30)//a>20&&a<=30

                {

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

                    {

                        sum += i;

                    }

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

                    {

                        sum += 10;

                    }

                }

                else if (a <= 40)//a>30&&a<=40

                {

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

                    {

                        sum += i;

                    }

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

                    {

                        sum += 10;

                    }

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

                    {

                        sum += 20;

                    }

                }

                else if (a <= 49)

                {

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

                    {

                        sum += i;

                    }

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

                    {

                        sum += 10;

                    }

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

                    {

                        sum += 20;

                    }

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

                    {

                        sum += 30;

                    }

                }

                else//a==50

                {

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

                    {

                        sum += i;

                    }

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

                    {

                        sum += 10;

                    }

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

                    {

                        sum += 20;

                    }

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

                    {

                        sum += 30;

                    }

                    sum += 100;

                }

            }

            else

            {

                Console.WriteLine("输入有误!");

            }

            Console.WriteLine("您的分数为:"+sum);

            Console.ReadLine();

法二:

            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 <= 20)

                    {

                        sum += i;

                    }

                    else if (i <= 30)//i>20&&i<=30

                    {

                        sum += 10;

                    }

                    else if (i <= 40)

                    {

                        sum += 20;

                    }

                    else if (i <= 49)

                    {

                        sum += 30;

                    }

                    else

                    {

                        sum += 100;

                    }

                }

            }

            else

            {

                Console.WriteLine("输入有误!");

            }

            Console.WriteLine("您的分数是:"+sum);

            Console.ReadLine();

4、篮球弹起问题,篮球初始所在高度是20米。每次弹起是上一次的五分之四,输入次数,求篮球弹起高度。

            Console.Write("请输入次数:");

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

            double h = 20;

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

            {

                h = h * 4 / 5;

            }

            Console.WriteLine("经过{0}次弹起,篮球高度是:{1}",a,h);

            Console.ReadLine();

5、折纸:有一张超大的纸,可以无限次折叠,厚度为0.07毫米。问,折叠多少次可以超过珠峰的高度(8848米)。

            int ci = 0;//标记变量,标记总共运行几次

            double h = 0.07;//8848m=8848000

            for (; ; )

            {

                ci++;

                h *= 2;//h=h*2;

                if (h > 8848000)

                {

                    break;

                }

            }

            Console.WriteLine("经过{0}次折叠,可以超过珠峰的高度。高度为:{1}米",ci,h/1000);

            Console.ReadLine();

二、for循环的嵌套例题

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

法一、

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

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

            int sum = 0;

            int jie = 1;

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

            {

                jie *= i;

                sum += jie;

            }

            Console.WriteLine("结果是:" + sum);

            Console.ReadLine();

法二、嵌套

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

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

            int sum = 0;

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

            {

                int jie = 1;

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

                {

                    jie *= j;

                }

                sum += jie;

            }

            Console.WriteLine("结果是:" + sum);

            Console.ReadLine();

2、九九乘法表

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

            {

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

                {

                    Console.Write("{0}*{1}={2} \t", j, i, i * j);

                }

                Console.WriteLine();

            }

            Console.ReadLine();

3、打印四个方向的三角形

●直角在左下方

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

            {

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

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

●直角在左上方

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

            {

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

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

●直角在右下方

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

            {

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

                {

                    Console.Write("  ");

                }

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

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

●直角在右上方

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

            {

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

                {

                    Console.Write("  ");

                }

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

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

3、打印菱形

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

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

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

            {

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

                {

                    Console.Write("  ");

                }

                for (int j = 1; j <=2*i-1; j++)

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

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

            {

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

                {

                    Console.Write("  ");

                }

                for (int j = 1; j <= 2 *(a-i) - 1; j++)

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

4、输入任意数,打印出对应三角形

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

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

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

            {

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

                {

                    Console.Write("★");

                }

                Console.WriteLine();

            }

            Console.ReadLine();

以上是关于C#语句二——循环语句(for循环与for循环嵌套)的主要内容,如果未能解决你的问题,请参考以下文章

在VB中 for 循环嵌套语句的用法语解释(必须清楚!!)

无限循环与嵌套循环

《C#零基础入门之百识百例》(十四)循环结构 for语句 -- 打印乘法表

for语句与if语句嵌套的简单应用

第三次实验报告

for语句,嵌套循环