zoukankan      html  css  js  c++  java
  • if练习随笔

    分支:——四种if
    一、
    if(条件表达式)
    {
    }

    二、
    if(条件表达式)
    {
    }
    else
    {
    }

    三、
    if(条件表达式)
    {
    }
    else if(条件表达式)
    {
    }
    else if(条件表达式)
    {
    }
    ....
    else
    {
    }

    四、
    if(条件表达式)
    {
     if(条件表达式)
     {
     }
     .....
    }
    else
    {
     if(条件表达式)
     {
     }
     .....
    }

    作业:1.输入年、月、日,判断输入的日期格式是否正确

    static void Main1(string[] args)
            {
                Console.WriteLine("输入年份");
                int year = Convert.ToInt32(Console.ReadLine());
                if (year >= 1 && year <= 9999)
                {
                    Console.WriteLine("输入月份");
                    int month = Convert.ToInt32(Console.ReadLine());
                    if (month >= 1 && month <= 12)
                    {
                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                            Console.WriteLine("输入日期");
                            int day = Convert.ToInt32(Console.ReadLine());
                            if (day >= 1 && day <= 31)
                            {
                                Console.WriteLine(year + "" + month + "" + day + "");
                            }
                            else
                            {
                                Console.WriteLine("错误,大月只能在1-31之间");
                            }
                        }
                        else if (month == 4 || month == 6 || month == 9 || month == 11)
                        {
                            Console.WriteLine("输入日期");
                            int day = Convert.ToInt32(Console.ReadLine());
                            if (day >= 1 && day <= 30)
                            {
                                Console.WriteLine(year + "" + month + "" + day + "");
                            }
                            else
                            {
                                Console.WriteLine("错误,小月只能在1-30之间");
                            }
                        }
                        else if (month == 2)
                        {
                            Console.WriteLine("输入日期");
                            int day = Convert.ToInt32(Console.ReadLine());
                            if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                            {
                                if (day >= 1 && day <= 29)
                                {
                                    Console.WriteLine(year + "" + month + "" + day + "");
                                }
                                else
                                {
                                    Console.WriteLine("打印:错误,闰年2月只能在1-29之间");
                                }
                            }
                            else
                            {
                                if (day >= 1 && day <= 28)
                                {
                                    Console.WriteLine(year + "" + month + "" + day + "");
                                }
                                else
                                {
                                    Console.WriteLine("打印:错误,平年2月只能在1-28之间");
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("打印年错误");
                }
            }

    2.做猜拳的游戏。
    0-剪刀,1-石头,2-布

    static void Main(string[] args)
            {
                Random rand = new Random();
                int computer = rand.Next(3);
                Console.Write("猜拳游戏,0-剪刀,1-石头,2-布
    请出拳:");
                int ren = Convert.ToInt32(Console.ReadLine());
                if (ren == 0) //人出剪刀
                {
                    if (computer == 0)
                    {
                        Console.WriteLine("平局");
                    }
                    else if (computer == 1)
                    {
                        Console.WriteLine("你输了");
                    }
                    else if (computer == 2)
                    {
                        Console.WriteLine("你赢了");
                    }
                }
                else if (ren == 1) //人出石头
                {
                    if (computer == 0)
                    {
                        Console.WriteLine("你赢了");
                    }
                    else if (computer == 1)
                    {
                        Console.WriteLine("平局");
                    }
                    else if (computer == 2)
                    {
                        Console.WriteLine("你输了");
                    }
                }
                else if (ren == 2) //人出布
                {
                    if (computer == 0)
                    {
                        Console.WriteLine("你输了");
                    }
                    else if (computer == 1)
                    {
                        Console.WriteLine("你赢了");
                    }
                    else if (computer == 2)
                    {
                        Console.WriteLine("平局");
                    }
                }
                else
                {
                    Console.WriteLine("你别乱出");
                }
            }
  • 相关阅读:
    LockFile文件-解决并发写入日志的问题
    二、Consul Service Mesh
    查看CPU和内存,用机器指令和汇编指令编程
    环境配置过程中的一些小tips
    工具使用指北:GDB
    瞧瞧我发现了什么
    新的目标:Capture The Flag
    python 实现的idw插值方法
    Python 利用 百度接口输入地点名字返回经纬度
    轻松搞定javascript变量(闭包,预解析机制,变量在内存的分配 )
  • 原文地址:https://www.cnblogs.com/languang/p/4476289.html
Copyright © 2011-2022 走看看