C# 分支语句 练习题

时间:2023-02-22 14:48:53

1、“请输入年份:”(1-9999)
“请输入月份:”(1-12)
“请输入日期:”(要判断大小月,判断闰年)
判断输入的时间日期是否正确

      bool dateISOK = false;//放置日期是否正确的判断
            bool isRN = false;//放置是否是闰年的判断
            int year = 0;
            int month = 0;
            int day = 0;

            #region  //判断年份是否正确
            Console.Write("请输入年份:");
            year = Convert.ToInt32(Console.ReadLine());

            if (year >= 1 && year <= 9999)
            {
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                {
                    isRN = true;
                }

                #region   //判断月份是否正确
                Console.Write("年份输入正确,请输入月份:");
                month = Convert.ToInt32(Console.ReadLine());
                if (month > 0 && month < 13)
                {
                    Console.Write("月份输入正确,请输入日期:");
                    //判断日期是否正确
                    day = Convert.ToInt32(Console.ReadLine());
                    #region  判断 1 3 5 7 8 10 12月份的日期
                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                    {
                        if (day >= 1 && day <= 31)
                        {
                            Console.WriteLine("输入的全部日期正确!");
                            dateISOK = true;
                        }
                        else
                        {
                            Console.WriteLine("输入的日期有误!");
                        }
                    }
#endregion
                    #region 判断 4 6 9 11 月份的天数正确
                    if (month == 4 || month == 6 || month == 9 || month == 11)
                    {
                        if (day >= 1 && day <= 30)
                        {
                            Console.WriteLine("输入的全部日期正确!");
                            dateISOK = true;
                        }
                        else
                        {
                            Console.WriteLine("输入的日期有误!");
                        }
                    }
                    #endregion
                    else if (month == 2)
                    {
                        #region  判断闰年的2月份
                        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                        {
                            if (day >= 1 && day <= 29)
                            {
                                Console.WriteLine("输入的全部日期正确!");
                                dateISOK = true;
                            }
                            else
                            {
                                Console.WriteLine("输入的日期有误!");
                            }
                        }
                        else
                        {
                            if (day >= 1 && day <= 28)
                            {
                                Console.WriteLine("输入的全部日期正确!");
                                dateISOK = true;
                            }
                            else
                            {
                                Console.WriteLine("输入的日期有误!不是闰年2月最多28天");
                            }
                        }
                        #endregion
                    }
                }
                else
                {
                    Console.WriteLine("输入的月份有误!");
                }
                #endregion
            }
            else
            {
                Console.WriteLine("输入的年份有误!");
            }
            #endregion

 

2、计算输入的时间是当前这一年的第几天

 

 

 //如果日期都正确,我们开始计算第几天
            if (dateISOK == true)
            {
                int count = 0;
                int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30, m12 = 31;

                if (month == 1)
                    count = day;
                if (month == 2)
                    count = m1 + day;
                if (month == 3)
                {
                    if (isRN == true)
                        count = m1 + m2 + 1 + day;
                    else
                        count = m1 + m2 + day;
                }
                if (month == 4)
                {
                    if (isRN == true)
                        count = m1 + m2 + 1 + m3 + day;
                    else
                        count = m1 + m2 + m3 + day;
                }
                if (month == 5)
                {
                    if (isRN == true)
                        count = m1 + m2 + 1 + m3 + m4 + day;
                    else
                        count = m1 + m2 + m3 + m4 + day;
                }

                Console.WriteLine(year + "" + month + "" + day + "日是此年的第" + count + "");
            }

 

 

这里的if  可以用switch   case

例如 :int m=1;//定义一个数据类型

       switch(m)//括号内是一个数据类型   m假设为 string类型 系统会报错

{     case 1: //当m值为1时       

       Console.WriteLine();//程序运行这里    break;//跳出去

       default:代码段;break;
       (default可有可无,对应else)

 

}


3、猜拳将用户的手势和电脑的手势输出出来

            #region 手势生成
            Console.Write("请输入您的手势(石头、剪刀、包袱):");
            string user = Console.ReadLine();
            int user1;

            if (user == "石头")
                user1 = 0;
            else if (user == "剪刀")
                user1 = 1;
            else
                user1 = 2;

            Random r = new Random();
            int com = r.Next(0, 3);
            #endregion

            #region 输出两位选手的手势
            string comEnd = "";

            if (user != "剪刀" && user != "石头")
            {
                user = "包袱";
            }

            if (com == 0)
                comEnd = "石头";
            else if (com == 1)
                comEnd = "剪刀";
            else
                comEnd = "包袱";

            Console.WriteLine("用户手势:" + user + "\t电脑手势:" + comEnd);
            #endregion

            #region 胜负判断
            if ((user1 == 0 && com == 1) || (user1 == 1 && com == 2) || (user1 == 2 && com == 0))
            {
                Console.WriteLine("用户胜利!");
            }
            else if ((user1 == 0 && com == 2) || (user1 == 1 && com == 0) || (user1 == 2 && com == 1))
            {
                Console.WriteLine("用户失败!");
            }
            else
            {
                Console.WriteLine("平局!");
            }
            #endregion

            Console.ReadLine();