2017-2-22 if语句 if语句的嵌套 以及课堂练习 经典猜拳游戏,闰年判断,输出当前时间的天数等

(一)if语句

1.格式   if(){       

    }else if()

    {

    }

  注意:如果if后面不写花括号,只执行下面第一句

(二)语句1:顺序语句

      2:循环语句

      3:分支语句

课后练习:

1.猜拳游戏(用目前所学的if语句进行简单的编写)

注释:猜拳游戏可以继续进行完善,目前只能输出用户,电脑的出拳方式并进行判断

  代码:

static void Main(string[] args)
        {
            Console.Write("请用户输入(0-石头 1-剪刀 2-包袱):");
            string user = Console.ReadLine();

            Random r = new Random();//生成随机数
            int com = r.Next(0, 3);
            //获取电脑出拳的结果,并显示
            if(com==0)
            {
                Console.WriteLine("电脑出石头");
            }else if(com==1)
            {
                Console.WriteLine("电脑出剪刀");
            }else if(com == 2)
            {
                Console.WriteLine("电脑出包袱");
            }

            //0 石头 1 剪刀 2  包袱
            //定义int类型,获取用户的输入
             int user1=0;
            

            if (user == "石头")
            {
                user1 = 0;
            }
            else if (user == "剪刀")
            {
                user1 = 1;
            }
            else if (user == "包袱")
            {
                user1 = 2;
            }
            // 用户 0  1 2           0  1   2
            // 电脑 1  2  0          2  0   1
            //进行猜拳的判断
            if (user1 - com == -1 || user1 - com == 2)
            {
             
                Console.WriteLine("用户胜利");
            }
            else if (user1 - com == -2 || user1 - com == 1)
            {
                Console.WriteLine("电脑胜利");

            }
            else if (user1 - com == 0)
            {
                Console.WriteLine("平局");
            }




            Console.ReadLine();
        }                        

2.闰年的判断:

熟悉闰年判断的条件:year % 100 == 0 && year % 4 == 0 || year % 400 == 0

3.24时的转换

代码:

Console.Write("请输入24小时制的时间:");
            int hour = Convert.ToInt32(Console.ReadLine());
            if (hour > 24 || hour < 0)
            {
                Console.WriteLine("您输入的时间有误。");
            }
            else 
            {
                if(hour<=6)
                {
                    Console.WriteLine("您输入的时间是凌晨"+hour+"点。");
                }else if(hour<=12)
                {
                    Console.WriteLine("您输入的时间是上午" + hour + "点。");
                }
                else if (hour >= 13 || hour < 22)
                {
                    int xiawu = hour - 12;
                    Console.WriteLine("您输入的时间是下午" + xiawu + "点。");
                }
                else 
                {
                    int shenye = hour - 12;
                    Console.WriteLine("您输入的时间是下午" + shenye + "点。");
                }
            
            
            
            }
                    




            Console.ReadLine();

4.简单的标准体重的算法

功能:用户输入性别,身高,体重。判断用户的体重是不是标准体重,并进行反馈

代码:

static void Main(string[] args)
        {
            Console.Write("请输入性别:");
            string sex = Console.ReadLine();
          

            if(sex == "")
            {
                Console.Write("请输入身高:");
                int height = Convert.ToInt32(Console.ReadLine());

                Console.Write("请输入体重(公斤):");
                int weight = Convert.ToInt32(Console.ReadLine());

                //用户标准的体重
                int bizozhun = height - 100;

                if (weight - bizozhun > 3 || bizozhun - weight > 3)
                {
                    Console.WriteLine("用户不是标准体重");
                }
                else 
                {
                    Console.WriteLine("用户是标准体重");
                }
                
            }

            if (sex == "")
            {
                Console.Write("请输入身高:");
                int height = Convert.ToInt32(Console.ReadLine());

                Console.Write("请输入体重(公斤):");
                int weight = Convert.ToInt32(Console.ReadLine());

                //用户标准的体重
                int bizozhun = height - 110;

                if (weight - bizozhun > 3 || bizozhun - weight > 3)
                {
                    Console.WriteLine("用户不是标准体重");
                }
                else
                {
                    Console.WriteLine("用户是标准体重");
                }

            }



            Console.ReadLine();
        }
原文地址:https://www.cnblogs.com/qingnianxu/p/6428752.html