2016.7.23C#语言基础 循环语句

循环语句   (2016-07-23) 


 

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

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

 

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

{  

  //循环体

}

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

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

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

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

  int sum=o;

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

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

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

 {

    sum+=i;

  }

    Console.Write("从1加到a的和是"+sum);

    Console.ReadLine();

 

2.输入一个整数,求阶乘的和

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

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

    int sum=0;

    int sum1=1;

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

    {

     sum*=i;  //sum=sum*i;

    //if (i == 4)//不想要4的    

    //{    

    //    continue;//终止本次循环,继续下次循环     

    //}

     sum1+=sum;

    }

     Console.WriteLine(sum1);      

     Console.ReadLine();

3.一个游戏,前20关是每一关自身的分数,
         21-30关每一关是10分
         31-40关,每一关是20分
         41-49关,每一关是30分
         50关,是100分
         输入你现在闯到的关卡数,求你现在拥有的分数

           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)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= a; i++)
                    {
                        sum += 10;
                    }
                }

               else if (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 <= 49; i++)
                    {
                        sum += 30;
                    }
                }

             else
                {
                    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.Write("你的分数是:"+sum);

            }            

              else            

             {                

              Console.Write("您输入的关卡有误!");           

              }

 

    4. 找100以内与7有关得数 并打印            

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

            {

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

                {            

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

                }       

            }

            Console.ReadLine();

 

posted on 2016-07-23 21:57  皮皮怪  阅读(222)  评论(0编辑  收藏  举报

导航