码迷,mamicode.com
首页 > 其他好文 > 详细

循环语句

时间:2016-06-22 17:13:11      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

for

格式:

  for (int i = 1/*初始条件*/; i <= 100/* 循环条件*/; i++ /*状态改变*/)

  {

  //循环体,执行代码;(break;跳出循环体)

  }

给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果满足则进入for语句执行,for语句内的代码执行完毕后,将按照状态改变,改变变量,然后判断是否符合循环条件,符合则继续执行for语句内的代码,直到变量i不符合循环条件则终止循环,或者碰到break;命令,直接跳出当前的for循环。

break在这里是跳出循环的意思。

for可以嵌套。

例:

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

1.if嵌套for

      Console.WriteLine("输入关卡");
            int a = int.Parse(Console.ReadLine());
            int b = 0;
            if (a <= 50 && a > 0)
            {
                if (a <= 20)
                {
                    for (int n = 0; n <= a; n++)
                    {
                        b = b + n;
                    }
                }
                else if (a <= 30)
                {
                    for (int n = 0; n <= 20; n++)
                    {
                        b = b + n;
                    }
                    for (int n = 21; n <= a; n++)
                    {
                        b = b + 10;
                    }
                }
                else if (a <= 40)
                {
                    for (int n = 0; n <= 20; n++)
                    {
                        b = b + n;
                    }
                    for (int n = 21; n <= 30; n++)
                    {
                        b = b + 10;
                    }
                    for (int n = 31; n <= a; n++)
                    {
                        b = b + 20;
                    }
                }
                else if (a <= 49)
                {
                    for (int n = 0; n <= 20; n++)
                    {
                        b = b + n;
                    }
                    for (int n = 21; n <= 30; n++)
                    {
                        b = b + 10;
                    }
                    for (int n = 31; n <= 40; n++)
                    {
                        b = b + 20;
                    }
                    for (int n = 41; n <= a; n++)
                    {
                        b = b + 30;
                    }
                }
                else
                {
                    for (int n = 0; n <= 20; n++)
                    {
                        b = b + n;
                    }
                    for (int n = 21; n <= 30; n++)
                    {
                        b = b + 10;
                    }
                    for (int n = 31; n <= 40; n++)
                    {
                        b = b + 20;
                    }
                    for (int n = 41; n <= 49; n++)
                    {
                        b = b + 30;
                    }
                    for (int n = 50; n <= a; n++)
                    {
                        b = b + 100;
                    }
                }
                Console.WriteLine("分数是" + b);
            }
            else
            {
                Console.WriteLine("输入有误");
            }
            Console.ReadLine();

2.for嵌套if


            Console.WriteLine("输入关卡");
            int a = int.Parse(Console.ReadLine());
            int b = 0;
            if (a <= 50 && a > 0)
            {
                for (int n = 0; n <= a; n++)
                {
                    if (n <= 20)
                    {
                        b = b + n;
                    }
                    else if (n <= 30)
                    {
                        b = b + 10;
                    }
                    else if (n <= 40)
                    {
                        b = b + 20;
                    }
                    else if (n <= 49)
                    {
                        b = b + 30;
                    }
                    else
                    {
                        b = b + 100;
                    }
                }
                Console.WriteLine("分数是" + b);
            }
            else
            {
                Console.WriteLine("输入有误");
            }
            Console.ReadLine();

for嵌套

            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 <= (5 - i) + 1; j++)
                {
                    Console.Write("□");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

□ □ □ □ □

 

□ □ □ □

 

□ □ □

 

□ □

 

 

 

 

 

 

 

            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j < i; j++)
                {
                    Console.Write("  ");
                }
                for (int k = 1; k <= (5 - i) + 1; k++)
                {
                    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 h = 1; h <= i; h++)
                {
                    Console.Write("□");
                }
                Console.WriteLine();
            }
            Console.ReadLine();

            □

         □ □

      □ □ □

   □ □ □ □

□ □ □ □ □

          

循环语句

标签:

原文地址:http://www.cnblogs.com/shi2172843/p/5606547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!