zoukankan      html  css  js  c++  java
  • 【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();
  • 相关阅读:
    mycat 1.6.6.1 distinct报错问题
    linux下Tomcat+OpenSSL配置单向&双向认证(自制证书)
    Too many open files错误与解决方法
    Tomcat类加载机制触发的Too many open files问题分析(转)
    spring boot 自签发https证书
    redis集群如何解决重启不了的问题
    centos7 docker 安装 zookeeper 3.4.13 集群
    centos7用docker安装kafka
    心怀感恩
    不使用if switch 各种大于 小于 判断2个数的大小
  • 原文地址:https://www.cnblogs.com/snow22546/p/6431174.html
Copyright © 2011-2022 走看看