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

7.23 学习 循环语句

时间:2016-07-24 16:19:46      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

循环语句

循环四要素:初始条件,循环条件,循环体,状态改变。
for(初始条件;循环条件;状态改变)
{
 循环体
}
break ——中断循环,跳出循环
continue——停止本次循环,进入下次循环

 .输入一个整数,计算从1加到这个数的结果
int sum = 0;
Console.WriteLine("请输入一个正整数");
int a = int.Parse(Console.ReadLine());
for(int i =0; i<=a ;i++)
{
 sum += i;
}
Console.WriteLine("从1加到"+a+"的结果是:"+sum);
Console.ReadLine();

            死循环
            for (; ; )
            {
                Console.WriteLine("Hello");
            }

 一个游戏,前20关是每一关自身的分数,
            21-30关每一关是10分
            31-40关,每一关是20分
            41-49关,每一关是30分
            50关,是100分
            输入你现在闯到的关卡数,求你现在拥有的分数
            两种做法:if嵌套for    for嵌套if
            Console.Write("请输入您现在闯到的关卡数:");
            int guan = int.Parse(Console.ReadLine());
            if (guan >= 1 && guan <= 50)
            {
                int sum = 0;
                if (guan <= 20)
                {
                    for (int i = 1; i <= guan; i++)
                    {
                        sum += i;
                    }
                }
                else if (guan <= 30)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= guan; i++)
                    {
                        sum += 10;
                    }
                }
                else if (guan <= 40)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= guan; i++)
                    {
                        sum += 20;
                    }
                }
                else if (guan <= 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 <= guan; i++)
                    {
                        sum += 30;
                    }
                }
                else//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;
                }
                Console.WriteLine(sum);
            }
            else
            {
                Console.WriteLine("输入有误!");
            }
            Console.ReadLine();

  

 

7.23 学习 循环语句

标签:

原文地址:http://www.cnblogs.com/fuze/p/5700854.html

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