博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7.23
阅读量:6672 次
发布时间:2019-06-25

本文共 2075 字,大约阅读时间需要 6 分钟。

循环语句

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

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

 

 //请输入一个正整数 求阶乘的和

            Console.Write("请输入一个正整数");
            int a = int.Parse(Console.ReadLine());
            int sum = 1;
            int sum1 = 0;
            for (int i = 1; i <= a; i++)
            {
                sum = sum + i;
            sum1 = sum * i;
           
            }

            Console.WriteLine(sum1);

            Console.ReadLine();

 

第一种:

            一个游戏,前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();

 

第二种

 

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

            int guan = int.Parse(Console.ReadLine());
            if (guan <= 50 && guan >= 1)
            {
                int sum = 0;
                for (int i = 1; i <= guan; i++)
                {
                    if (i <= 20)
                    {
                        sum += i;
                    }
                    else if (i <= 30)
                    {
                        sum += 10;
                    }
                    else if (i <= 40)
                    {
                        sum += 20;
                    }
                    else if (i <= 49)
                    {
                        sum += 30;
                    }
                    else
                    {
                        sum += 100;
                    }
                }
                Console.WriteLine(sum);
            }
            else
            {
                Console.WriteLine("输入有误!");
            }
            Console.ReadLine();

 

 

            //找出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 % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                {
                    Console.Write(i+"\t");
                }
            }
            Console.ReadLine();

 

转载于:https://www.cnblogs.com/power8023/p/5699087.html

你可能感兴趣的文章
maven编译时出现There are test failures
查看>>
SpringBoot | 第三十一章:MongoDB的集成和使用
查看>>
网络学习笔记2
查看>>
JPA--多对多关系
查看>>
配置sharepoint 2010错误:Microsoft.SharePoint.Upgrad...
查看>>
UUID 生成算法JS版
查看>>
JAVA中,Map转实体类、实体类转Map的方法
查看>>
获取n!的末尾有多少个0?
查看>>
使用递归遍历并转换树形数据(以 TypeScript 为例)
查看>>
PHP类推荐:QueryList|基于phpQuery的无比强大的PHP采集工具
查看>>
windows下实现wamp与tomcat环境整合
查看>>
我的友情链接
查看>>
Windows Server 2012 R2搭建IIS服务器
查看>>
SCVMM 2012 R2运维管理二之:安装域控制器
查看>>
[Fibre Channle 实战之三]FC 和iSCSI的使用差异
查看>>
c#winform选择文件,文件夹,打开指定目录方法
查看>>
traceroute
查看>>
如何划分man文档的章节
查看>>
微信公众号的分类
查看>>
分布式高可用存储(drbd+corosync+pacemaker+MooseFS)
查看>>