• 循环语言(for)


           循环语句:

          给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果满足则进入for语句循环,for语句内的代码执行完毕之后,将按照状态改变改变变量,然后判断是否符合循环条件,符合继续执行循环,知道变量i变得不符合条件为止或者遇见break跳出循环。

    模版:

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

    执行

    输入5的阶乘

          Int sum=0;

          Int jie=1;

          For (i=1;i<=5;i++)

          {

          Jie*=i;

          Sum+=jie

          }

          Console.WriteLine("5的阶乘是"+sum);

    输入关卡数,得出分数

    方法一:

              /* Console.Write("请输入关卡数:");

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

                int n = 0;

                {

                    if (a > 50&&a<1)

                    {

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

                    }

                    else

                    {

                        if (a <= 20)

                        {

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

                                n = n + i;

                        }

                        else if (a <= 30)

                        {

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

                                n = n + i;

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

                            n = n + 10;

                        }

                        else if (a <= 40)

                        {

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

                                n = n + i;

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

                                n = n + 10;

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

                            n = n + 20;

                        }

                        else if (a <= 49)

                        {

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

                                n = n + i;

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

                                n = n + 10;

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

                                n = n + 20;

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

                            n = n + 30;

                        }

                        else

                        {

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

                                n = n + i;

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

                                n = n + 10;

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

                                n = n + 20;

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

                                n = n + 30;

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

                            n = n + 100;

                        }

                              Console.WriteLine("您输入的关卡积分是:" + n);

                    }

                }

                 Console.ReadLine();

    方法二:

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

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

                int n = 0;

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

                {

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

                        if (i <= 20)

                    {

                        n += i;

                    }

                    else if (i <= 30)

                    {

                        n = n + 10;

                    }

                    else if (i <= 40)

                    {

                        n = n + 20;

                    }

                    else if (a <= 49)

                    {

                        n = n + 30;

                    }

                    else if (a == 50)

                    {

                        n = n + 100;

                    }

                }

                else

                {

                    Console.WriteLine("输入错误");

                }

                    Console.WriteLine("您输入的关卡积分是:" + n);

                Console.ReadLine();*/

    篮球在20米的高空放下,每次弹起的高度是上一次的五分之四

                Console.Write("请输入篮球弹起的次数:");

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

                double sum = 20;

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

                {

                    sum *=0.8;

                }

                Console.WriteLine("高度是:"+sum);

                Console.ReadLine();*/

     一张纸的厚度是0.07毫米,假设可以无限次的折叠,请问折叠多少次之后可以超过珠穆朗玛峰的高度8848米?

                //Console.WriteLine("请问折纸多少次可以超过珠峰的高度?");

                //int i = 0;

                //double h = 0.07;

                //for (;;)

                //{

                //    ++i;

                //    h*=2;

                //    if (h > 8848000)

                //    {

                //        break;

                //    }

                //}

                //Console.WriteLine("折纸的次数是:{0},超过珠峰高度。",i);

                //Console.ReadLine();

    99乘法表

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

                //{

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

                //    {

                //        int s = 1;

                //        s = j * i;

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

                //    }

                //   Console.WriteLine();// Console.Write( );

                //}

                //Console.ReadLine();

                //for (int i = 0; i < 5; ++i)

                //{

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

                //    {

                //        Console.Write("▲");

                //    }

                //    Console.WriteLine();

                //}

                //Console.ReadLine();

                //for (int i = 0; i < 5; ++i)

                //{

                //    for (int j = 0; j < i; ++j)

                //    {

                //        Console.Write("  ");

                //    }

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

                //    {

                //        Console.Write("▲");

                //    }

                //    Console.WriteLine();

                //}

                //Console.ReadLine();

    输入一个数字,形成直角在左下角,高宽军事这个数字的三角形

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

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

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

                //{

                //    for (int j = 0; j <= i; ++j)

                //    {

                //        Console.Write("▲");

                //    }

                //    Console.WriteLine();

                //}

                //Console.ReadLine();

    //输入0到100以内的数字,找出其中与7相关的数字。

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

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

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

                {

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

                        Console.Write("  "+i);

                }

                Console.ReadLine();

  • 相关阅读:
    枚举工具类:封装判断是否存在这个枚举
    MYSQL插入emoji报错解决方法Incorrect string value
    文件大小转换带上单位工具类(文件byte自动转KBMBGB)
    mysql 统计七天数据并分组
    mybatis plus 和 druid 版本导致LocalDateTime 不兼容问题
    Layui弹框中select下拉列表赋值回显
    查看环境版本
    Linux 常用命令
    安装jdk14的坑
    modbus_tk解析
  • 原文地址:https://www.cnblogs.com/light3857/p/5607606.html
Copyright © 2020-2023  润新知