• 循环、类0311


    循环、类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(" ");

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

                Console.Write(d);

                Console.Write(" ");

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

                Console.Write(e);

                Console.Write(" ");

                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取下限

  • 相关阅读:
    English 2
    速算24点
    心理学1
    从微服务到函数式编程
    034 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 01 流程控制概述
    033 01 Android 零基础入门 01 Java基础语法 03 Java运算符 13 运算符和表达式知识点总结
    032 01 Android 零基础入门 01 Java基础语法 03 Java运算符 12 运算符和if-else条件语句的综合案例——闰年问题
    031 01 Android 零基础入门 01 Java基础语法 03 Java运算符 11 运算符的优先级
    030 01 Android 零基础入门 01 Java基础语法 03 Java运算符 10 条件运算符
    029 01 Android 零基础入门 01 Java基础语法 03 Java运算符 09 逻辑“非”运算符
  • 原文地址:https://www.cnblogs.com/zst062102/p/5267296.html
Copyright © 2020-2023  润新知