【2017-2-21】C#分支语句,分支嵌套,变量的作用域

分支语句

句式:if else(必须是if开头,可以是else if或者else结束,也可以直接结束)

if(bool型比较表达式)

{

如果上面的条件成立,则执行这里面的代码

}

else if(bool型比较表达式)

{

如果上面的条件成立,则执行这里面的代码

}

else(必须为空,不能写比较表达式)//只要上面条件都不成立,那么必走else里的代码

{

}

每一行最左侧红点称为断点,选中后表示即将执行本行代码;可以配合逐语句查看每一行代码的执行情况;

多个if从句之间为并列关系;

如果if,else if,else里面只有一句代码,则可以省略{};

新建一个随机变量

Random x=new Random();

int a=x.Next(0,n)

表示从0到n-1这n个数中随机抽取一个

            Console.Write("请输入您的手势 (石头/剪子/包袱)");
            string gesture = Console.ReadLine();
            int user;
            if (gesture=="石头")
            {
                user = 1; 
            }
            else if (gesture == " 剪子")
            {
                user = 2;
            }
            else 
            {
                user = 3;
            } 
            Random r = new Random();
            int cp = r.Next(1,4);
            if (user == cp)
            { 
                Console.WriteLine("平局");
            }
            else if (user == cp + 1 || user == cp - 2)
            {
                Console.WriteLine("电脑赢了");
            }
            else
            {
                Console.WriteLine("用户赢了");
            }

            Console.ReadLine();

 

 

分支嵌套

在if或者else if执行代码段里面继续插入if else分支语句;

表示满足该条件的状况下,继续进行其他条件的判断;

 

作用域

在main函数中,对象的作用域为他所在的最近的一对花括号内;

同一个作用域里面,不能重复定义同一个变量;

 

练习题1:

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

            Console.Write("请输入年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            if (year <= 0 || year > 9999)
            {
                Console.Write("您输入的年份有误");
            }
            else
            {
                Console.Write("请输入月份:");
                int month = Convert.ToInt32(Console.ReadLine());
                if (month < 1 || month >12)
                {
                    Console.Write("您输入的月份有误");
                }
                else
                {
                    Console.Write("请输入日期:");
                    int day = Convert.ToInt32(Console.ReadLine());
                    if (day > 31||day<1)
                    {
                        Console.Write("您输入的日期有误");
                    }
                    else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 0 && day <= 31))
                    {
                        Console.WriteLine("您输入的日期正确");
                    }
                    else if ((month == 4 || month == 6 || month == 9 || month == 11) && (day > 0 && day <= 30))
                    {
                        Console.WriteLine("您输入的日期正确");
                    }
                    else if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    {
                        if(month == 2 && (day > 0 && day <= 29))
                        {
                            Console.WriteLine("您输入的日期正确");
                        }
                        else
                        {
                            Console.WriteLine("您输入的日期有误");
                        }
                    }
                    else
                    {
                        if(month == 2 && (day > 0 && day < 29))
                        {
                            Console.WriteLine("您输入的日期正确");
                        }
                        else
                        {
                            Console.WriteLine("您输入的日期有误");
                        }

                    }
                }     
            }
            
            Console.ReadLine();

 

 

练习题2:

标准体重
男士体重 = 身高 - 100 +-3
kg cm
女士体重 = 身高 - 110 +-3

            Console.WriteLine("标准体重");
            Console.WriteLine("男士体重(kg)=身高(cm)-100+-3");
            Console.WriteLine("女士体重(kg)=身高(cm)-110+-3");
            Console.Write("请输入您的性别:");
            string sex = Console.ReadLine();
            Console.Write("请输入您的身高:");
            int height = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入您的体重:");
            int weight = Convert.ToInt32(Console.ReadLine());
            if (sex == "")
            {
                if (height - weight - 100 <= 3 && height - weight - 100 >= -3)
                {
                    Console.WriteLine("恭喜您属于标准体重!");
                }
                else
                {
                    Console.WriteLine("不好意思,您未达到标准体重!");
                }
            }
            else
            {
                if (height - weight - 110 <= 3 && height - weight - 110 >= -3)
                {
                    Console.WriteLine("恭喜您属于标准体重!");
                }
                else
                {
                    Console.WriteLine("不好意思,您未达到标准体重!");
                }
            }



            Console.ReadLine();

 

转载于:https://www.cnblogs.com/snow22546/p/6431174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值