文档章节

循环、类0311

badboy之1993
 badboy之1993
发布于 2016/05/12 14:08
字数 978
阅读 1
收藏 0

循环、类0311

 

1、棋盘放粮食/一天一分钱问题

          Console.Write("输入天数:");

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

            double sum = 0, d = 0;

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

            {

                if (i == 1)

                {

                    d = 1;

                    sum = 1;

                }

                else

                {

                    d = d * 2;

                    sum = sum + d;

                }

                Console.WriteLine("第" + i + "天给" + d + "分,这段时间总共赚了" + sum + "分");

            }

 

 

 

2、             兔子生兔子

        Console.Write("几个月后:");

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

            int cheng=0, xiao=0, you=0, zong=0;

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

            {

                if (i == 1)

                {

                    cheng = 0;

                    xiao = 0;

                    you = 1;

                    zong = 1;

                }

                else

                {

                    cheng = xiao+cheng;

                    xiao = you;

                    you = cheng;

                    zong=cheng+xiao+you;

                }

                Console.WriteLine("第" + i + "月总兔为" + zong + "对,成兔" + cheng + "对,小兔" + xiao + "对,幼兔" + you + "对。");

            }

 

 

 

3、             折纸:折多少次和珠穆朗玛峰一样高或超过

        int a = 7;

            int b = 884800000;

            int i=1;

            while (true)

            {

                a *= 2;

                if (a >= b)

                {

                    Console.WriteLine(i);

                    Console.WriteLine(a);

                    break;

                }

                Console.WriteLine("第" + i + "次折叠高度为" + a);

                i++;

            }

 

 

 

 

4、异常语句try catch finally

try保护执行里面的代码段,若其中一句有错误,直接跳转到catch

catch   try中发现异常,直接执行,若try中无措,不执行。

finally不管上面有没有错,都执行。

          try

            {

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

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

                Console.WriteLine("正确!");

            }

            catch

            {

                Console.WriteLine("你输入的有误。");

            }

            finally

            {

                Console.WriteLine("再接再厉!");

            }

 

 

 

5、         在输入有误的情况下,再重新输入

//情书1.0版

for (; ; )

            {

                Console.WriteLine("你爱不爱我?");

                string m = Console.ReadLine();

                if (m == "爱")

                {

                    Console.WriteLine("嗯,我也爱你!");

                    break;

                }

                else

                {

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

                }

            }

 

//情书2.0版

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

            {

                if (i == 1)

                {

                    Console.WriteLine("你爱不爱我?");

                }

                if (i == 2)

                {

                    Console.WriteLine("我问你呢,你到底爱比爱我?");

                }

                if (i == 3)

                {

                    Console.WriteLine("重要的事情问三遍,你爱不爱我?");

                }

                string m = Console.ReadLine();

                if (m == "爱")

                {

                    Console.WriteLine("嗯,我也爱你!");

                    System.Threading.Thread.Sleep(2500);

                    Console.WriteLine("从此王子和公主过上了没羞没躁的生活。");

                    break;

                }

                else

                {

                    if (i == 3)

                    {

                        Console.WriteLine("滚!");

                    }

                }

            }

 

 

6、         正确打印出年月日格式,若不对,再次循环询问年或月或日

         Console.Write("请输入年份:");

            for (; ; )

            {

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

                if (y >= 1 && y <= 9999)

                {

                    Console.Write("请输入月份:");

                    for (; ; )

                    {

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

                        if (m >= 1 && m <= 12)

                        {

                            Console.Write("请输入日期:");

                            for (; ; )

                            {

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

                                if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)

                                {

                                    if (d >= 1 && d <= 31)

                                    {

                                        Console.WriteLine("你输入的是" + y + "年" + m + "月" + d + "日");

                                        break;

                                    }

                                }

                                if (m == 4 || m == 6 || m == 9 || m == 11)

                                {

                                    if (d >= 1 && d <= 30)

                                    {

                                        Console.WriteLine("你输入的是" + y + "年" + m + "月" + d + "日");

                                        break;

                                    }

                                }

                                if (m == 2)

                                {

                                    if ((y % 4 == 0 && y % 100 != 0) || y % 100 == 0)

                                    {

                                        if (d >= 1 && d <= 29)

                                        {

                                            Console.WriteLine("你输入的是" + y + "年" + m + "月" + d + "日");

                                            break;

                                        }

                                    }

                                    if (!((y % 4 == 0 && y % 100 != 0) || y % 100 == 0))

                                    {

                                        if (d >= 1 && d <= 28)

                                        {

                                            Console.WriteLine("你输入的是" + y + "年" + m + "月" + d + "日");

                                            break;

                                        }

                                    }

                                    else

                                    {

                                        Console.Write("你输入的日期格式错误,请重新输入日期:");

                                    }

                                }

                                else

                                {

                                    Console.Write("你输入的日期格式错误,请重新输入日期:");

                                }

                            }

                            break;

                        }

                        else

                        {

                            Console.Write("你输入的月份格式错误,请重新输入月份:");

                        }

                    }

                    break;

                }

                else

                {

                    Console.Write("你输入的年份格式错误,请重新输入年份:");

                }

            }

 

 

 

7、         String类

        string a = "    abcdefg    ";

            int b = a.Length;//长度

            Console.WriteLine(b);

            string c = a.Trim();//去掉空格

            Console.Write(c);

            Console.Write("\n");

            string d = a.TrimStart();//去掉前边的空格

            Console.Write(d);

            Console.Write("\n");

            string e = a.TrimEnd();//去掉后边的空格

            Console.Write(e);

            Console.Write("\n");

            string f = a.ToUpper();//全部变大写

            Console.WriteLine(f);

            string g = a.ToLower();//全部变小写

            Console.WriteLine(g);

            string h = a.Substring(4);//只起始位置4,可以截取到尾

            Console.WriteLine(h);

            Console.WriteLine(a.Substring(5));//只起始位置5,可以截取到尾

            //a = a.Substring(6);//若果不重新赋值,a是没有变化的

            //Console.WriteLine(a);

            string i = a.Substring(4, 3);//只起始位置4,截取3个长度

            Console.WriteLine(i);

            int j = a.IndexOf("c");//第一次出现此字符串的索引

            Console.WriteLine(j);

            int k = a.LastIndexOf("c");//最后一次出现此字符串的索引

            Console.WriteLine(k);

            bool l = a.StartsWith("a");//是否以此字符串开始

            Console.WriteLine(l);

            bool m = a.EndsWith(" ");//是否以此字符串结尾

            Console.WriteLine(m);

            bool n = a.Contains("e");//是否包含此字符串

            Console.WriteLine(n);

            string p = a.Replace("d","n");//用n替换d

     Console.WriteLine(p);

    

 

8、         Math类

        double a = 5.5;

            Console.WriteLine(Math.Ceiling(a));//取上限

            Console.WriteLine(Math.Floor(a));//取下限

            Console.WriteLine(Math.PI*a);//π值

            Console.WriteLine(Math.Sqrt(a));//开平方根

     Console.WriteLine(Math.Round(a));//四舍五入,奇数.5去上限;偶数.5取下限

 

© 著作权归作者所有

上一篇: MySQL增删改查
下一篇: 静态关键字static
badboy之1993
粉丝 0
博文 42
码字总数 32545
作品 0
淄博
私信 提问
在eclipse上部署openfire 3.9.1源码,並配置openfire

在网上找了很久部署openfire源码的文章,由于我使用的是最新的3.9.1源码,试了很多种部署方法,都无法使用。后来看到下面这兩篇文章http://blog.csdn.net/StillCity/article/details/169883...

cywaker
2014/03/06
0
2
临摹写实日历图标的收获

个人临摹的效果图 详细教程如下: http://www.uisdc.com/photoshop-classic-calendar-icon PS上碰到的一些问题: 1.ps如何自动圆角? ps形状如何拐角? 原来涉及的问题是在:ps如何设置描边线...

洛根森
2017/03/21
0
0
红薯,你干了什么?黑了别人网站?

下面的图片都这样了 http://tech.it168.com/a2011/0311/1164/000001164773.shtml

戴威
2011/04/15
2.5K
24
网上刷单被骗了怎么办报警钱能回来吗

这个办法可以帮你把被骗的钱追回,要是你被骗了可以试试联系反网络诈骗电话0311一8754一8496我前几天也被骗就是通过这个电话追回来的,把被骗经历说给他们,说的越清楚,处理的结果就越快。真...

一直在北漂
2018/04/02
0
0
石家庄招ASP.NET程序员

1. 河北未来网络科技 2. ASP.NET程序员 职位描述 负责大型B2B商业网站的开发与维护 职位要求 1、一年以上大中型网站开发经验; 2、熟练ASP.net(C#),熟练使用Visual Studio开发工具,熟悉....

guke01
2010/09/25
329
5

没有更多内容

加载失败,请刷新页面

加载更多

识别图片内容,并将相应内容写到对应文本文件中

# -*- coding: utf-8 -*-"""Created on Thu Apr 18 17:05:47 2019@author: HeyJude"""import timestart_time = time.time()def GetText(pic_path, text_path): import pytesser......

KYO4321
1分钟前
0
0
Java多线程之创建线程的三种方式比较

一:继承Thread类创建线程 1:继承Thread类定义线程子类; 2:重写run()方法,定义线程的操作; 3:通过创建的线程子类对象.start() 启动线程。 package com.thread; public class First...

天王盖地虎626
4分钟前
0
0
inner join 与 left join 之间的区别

关于inner join 与 left join 之间的区别,以前以为自己搞懂了,今天从前端取参数的时候发现不是预想中的结果,才知道问题出在inner join 上了。 需求是从数据库查数据,在前端以柱形图的形式...

dragon_tech
5分钟前
0
0
linux下cpio.gz文件的解压方法

linux下cpio.gz文件的解压方法 linux下cpio.gz文件的解压方法linux解压cpiocpio.gz 今天下载了 10201_database_linux_x86_64.cpio.gz 文件,解压方法如下: 1. gunzip 10201_database_linux...

突突突酱
7分钟前
0
0
Shell分析服务器日志,解锁各种新姿势

Shell分析服务器日志,解锁各种新姿势 DevOps技术栈 5月10日 作者:Panda 原文:https://segmentfault.com/a/1190000009745139 自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服...

linzhuangrong
9分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部