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("你别乱出");
            }
        }
原文地址:https://www.cnblogs.com/languang/p/4476289.html