zoukankan      html  css  js  c++  java
  • switch case

    Console.WriteLine("1.汉堡包");
                Console.WriteLine("2.薯条");
                Console.WriteLine("3.鸡块");
                Console.WriteLine("4.鸡腿");
                Console.WriteLine("5.鸡米花");
    
                Console.Write("请输入您的选择项目数字:");
                string a = Console.ReadLine();
    
                switch (a)
                { 
                    case "1":
                        Console.WriteLine("您选择的是汉堡包!");
                        break;
                    case"2":
                        Console.WriteLine("您选择的是薯条!");
                        break;
                    case"3":
                        Console.WriteLine("您选择的是鸡块!");
                        break;
                    case "4":
                        Console.WriteLine("您选择的是鸡腿!");
                        break;
                    case "5":
                        Console.WriteLine("您选择的是鸡米花!");
                        break;
                    default:
                        Console.WriteLine("输入有误!");
                        break;
    }
    复制代码

    switch case就是一种选择语句

    逻辑性思维题:

    例:

    判断是不是闰年,普通年份,是4的倍数但是不能是100的倍数
    世纪年需要是400的倍数

    输入一个年份,判断是不是闰年

    Console.Write("请输入一个年份:");
    int year = int.Parse(Console.ReadLine());
    if (year >= 0 && year <= 9999)

    复制代码
                Console.Write("请输入一个年份:");
                int year = int.Parse(Console.ReadLine());
                if (year >= 0 && year <= 9999)
                {
                    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                    {
                        Console.WriteLine("您输入的年份是闰年!");
                    }
                    else
                    {
                        Console.WriteLine("您输入的年份是平年!");
                    }
                }
                else
                {
                    Console.WriteLine("您所输入的年份不正确。");
                }
    复制代码

    这个例题还算好,只要知道闰年是怎么回事就行了。

    例:

    输入年月日,看看格式是否正确

    做了三遍依旧有错误。

    复制代码
    Console.Write("请输入一个年份:");
                int year = int.Parse(Console.ReadLine());
                if (year >= 0 && year <= 9999)
                {
                    Console.Write("请输入月份:");
                    int m = int.Parse(Console.ReadLine());
                    if (m >= 1 && m <= 12)
                    {
                        Console.Write("请输入日期:");
                        int day = int.Parse(Console.ReadLine());
                        if (day >= 1 && day <= 31)
                        {
                            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 10 || m == 12)
                            {
                                Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                            }
                            if(m==4||m==6||m==9||m==11)
                            {
                                if(day<=30)
                                {
                                     Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期有误!");
                                }
                            }
                            else//剩下的二月份
                            {
                                if(year%4==0&&year%100!=0||year%400==0)//闰年的情况
                                {
                                    if(day<=29)
                                    {
                                         Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                                    }
                                    else
                                    {
                                        Console.WriteLine("您输入的日期格式有误!");
                                    }
                                }
                                else//平年
                                {
                                     if(day<=28)
                                    {
                                         Console.WriteLine("您输入的日期格式正确,"+year+""+m+""+day);
                                    }
                                    else
                                    {
                                        Console.WriteLine("您输入的日期格式有误!");
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("您输入的日期有误!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("您输入的月份各式有误!");
                    }
                         
                }
                     
                else
                {
                    Console.WriteLine("您输入的年份格式不正确!");
                }
    
                Console.ReadLine();
  • 相关阅读:
    Day20:Decorator and Metaclass
    Day19:hasattribute;getattribute;seattributet;delattribute
    Day18:polymorphic and reflection
    Day17:call the base class
    ACL权限
    内置函数
    用户配置文件-影子文件
    用户配置文件-用户信息文件
    脚本安装包
    定义函数
  • 原文地址:https://www.cnblogs.com/dianfu123/p/5267850.html
Copyright © 2011-2022 走看看