if条件语句

第四天

XMind 思维导图复习之前知识

数据类型-变量常量-运算符(表达式)-语句(顺序、分支、循环)-数组-函数

1.if语句格式

if(表达式)

{

       语句

}

 

注意:

1.如果,表达式成立,只执行一条语句的话,可以省去花括号。如果表达式成立,需要执行的语句数量大于等于2条,必须把这些句语放在花括号中。

2.if的小括号后,不要加分号。

 

2. if...else...格式

if(表达式)

{

}

else

{

}

 

注意:同if

1.可以有if没有else,但是有else,前面必须有if.

2.else后面没有括号和条件表达式。

3.满足条件走if后的花括号,直接就忽略else

 

3.多分支

if... else if .... else if.....else

if(表达式1)

{

       语句1;

}

else if(表达式2)

{

       语句2;

}

...

else

{

       语句n;

}

 

作业:年龄段;不同时间段的问候语;输入月份显示多少天。

 

4.if嵌套

if(表达式)

{

       if(表达式)

       {

       }

}

else

{

       if(表达式)

       {

       }

       else

       {

       }

}

 

 

作业:

1.输入年份,月份显示这个月有多少天?

 static void Main(string[] args)
        {
            //输入年份,月份显示这个月有多少天?
            Console.WriteLine("请输入年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入月份:");
            int month = Convert.ToInt32(Console.ReadLine());
            if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                Console.WriteLine("这个月是31天");
            }
            else if (month==4||month==6||month==9||month==11)
            {
                Console.WriteLine("这个月是30天");
            }
            else if (month==2)
            {
                if (year%400==0||(year%100!=0&&year%4==0))
                {
                    Console.WriteLine("这个月是29天");
                }
                else
                {
                    Console.WriteLine("这个月是28天");
                }
               
            }
            else
            {
                Console.WriteLine("输入月份有问题");
            }
        }

 

2.输入年份,月份,天,判断这个日期是否正确?(试着做)

static void Main(string[] args)
        {
            //输入年份,月份,天,判断这个日期是否正确?(试着做)
            int year, month, day;
            Console.WriteLine("输入年:");
            year = Convert.ToInt32(Console.ReadLine());
            if (year>=-9999&&year<=9999)
            {
                //年份正确
                Console.WriteLine("输入月份:");
                month = Convert.ToInt32(Console.ReadLine());
                if (month>=1&&month<=12)   
                {   
                    //月份正确
                    //月份正确,判断是大月,小月,2月
                    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                    {
                        //判断天数是否正确
                        Console.WriteLine("输入天:");
                        day = Convert.ToInt32(Console.ReadLine());
                        if (day>=1&&day<=31)
                        {
                            //日期正确
                            Console.WriteLine("日期正确");
                        }
                        else
                        {
                            //日期错误
                            Console.WriteLine("日期错误");
                        }
                    }
                        else if (month==4||month==6||month==9||month==11)
                    {
                        Console.WriteLine("输入天:");
                        day = Convert.ToInt32(Console.ReadLine());
                            if (day >= 1 && day <= 30)
                            {
                                //日期正确
                                Console.WriteLine("日期正确");
                            }
                            else
                            {
                                //日期错误
                                Console.WriteLine("日期错误");
                            }
                        }
                        
                    else
                        {
                            //二月,判断是否是闰年
                            if (year%400==0||year%100!=0&&year%4==0)
                            {
                               
                                Console.WriteLine("输入天:");
                                day = Convert.ToInt32(Console.ReadLine());
                                 if (day >= 1 && day <= 29)
                            {
                                //日期正确
                                Console.WriteLine("日期正确");
                            }
                            else
                            {
                                //日期错误
                                Console.WriteLine("日期错误");
                            }
                           }
                            else
                           {   //平年2月
                               Console.WriteLine("输入天:");
                               day = Convert.ToInt32(Console.ReadLine());
                              if (day >= 1 && day <= 28)
                            {
                                //日期正确
                                Console.WriteLine("日期正确");
                            }
                            else
                            {
                                //日期错误
                                Console.WriteLine("日期错误");
                            }

                            }
                        }
                }
 
                else
                 {
                    //月份错误
                     Console.WriteLine("月份错误");
                 }
            
           
        }
            else
            {
               //年份错误
                Console.WriteLine("年份错误");
            }
        }

 

3.axx+bx+c==0(a!=0)。输入a,b,c给这个一元二次方程,显示根的个数?

 static void Main(string[] args)
        {
            Console.WriteLine("一元二次方程a*x*x+b*x+c=0,(a!=0)请输入a,b,c的值");
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            int c = Convert.ToInt32(Console.ReadLine());
            double de = b * b - 4 * a * c; ;
           
            if (de>0)
            {
                Console.WriteLine("这个方程有两个不等实根");
            }
            else if (de==0)
            {
                Console.WriteLine("这个方程有两个相同的实根");
            }
            else
            {
                Console.WriteLine("这个方程没有实数根");
            }
        }

 

4.男士身高与体重的关系是:身高-100=体重; 女士:身高-110=体重。(自己试着做)

       上下浮动3公斤属正常。

       输入性别,身高,体重,输出:正常?偏胖?偏瘦?

static void Main4444(string[] args)
        { //男士身高与体重的关系是:身高-100=体重; 女士:身高-110=体重。(自己试着做)
            //上下浮动3公斤属正常。
            //输入性别,身高,体重,输出:正常?偏胖?偏瘦?
            string sex;
            int shengao, tizhong;

            //输入
            Console.WriteLine("性别:");
            sex = Console.ReadLine();
            Console.WriteLine("身高:");
            shengao = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("体重:");
            tizhong = Convert.ToInt32(Console.ReadLine());

            //运算输出

            if (sex == "")
            {
                int nbz = shengao - 100;
                if (nbz - tizhong >= -3 && nbz - tizhong <= 3)
                {
                    Console.WriteLine("体重正常,继续保持");
                }
                else if (nbz - tizhong < -3)
                {
                    Console.WriteLine("偏胖,注意饮食,加强锻炼");
                }
                else
                {
                    Console.WriteLine("偏瘦,注意营养");
                }
            }
            else if (sex == "")
            {
                int vbz = shengao - 110;
                if (vbz - tizhong >= -3 && vbz - tizhong <= 3)
                {
                    Console.WriteLine("体重正常,继续保持");
                }
                else if (vbz - tizhong < -3)
                {
                    Console.WriteLine("偏胖,注意饮食,加强锻炼");
                }
                else
                {
                    Console.WriteLine("偏瘦,注意营养");
                }
            }
            else
            {
                Console.WriteLine("输入错误");
            }
        }

 

5.做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布

要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。

                    

                     计算机生成随机数:

                     Random rand = new Random();

            int c = rand.Next(3);

 static void Main(string[] args) 
        {
           // 做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布
           //要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。
            //计算机出拳
            Console.WriteLine("0-剪刀,1-石头,2-布");
            Random r = new Random();
            int c = r.Next(3);
            //人出拳
            Console.WriteLine("请出拳:");
            int ren = Convert.ToInt32(Console.ReadLine());
            //比较
            if (c==0)
            {
                if (ren==0)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("平局");
                    
                }
                    else if (ren==1)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("你赢了!");
                }
                   else if (ren==2)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("电脑赢了,你输了");
                }
                    else
                {
                Console.WriteLine("输入错误!请按规矩出牌");
                } 
             

            }
            else if (c==1)
            {
                if (ren == 0)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("电脑赢了");
                }
                else if (ren == 1)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("平局");
                }
                else if (ren == 2)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("你赢了");
                }
                else
                {
                    Console.WriteLine("输入错误!请按规矩出牌");
                } 
            }
            else if (c==2)
            {
                if (ren == 0)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("你赢了");
                }
                else if (ren == 1)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("电脑赢了,你输了");
                }
                else if (ren == 2)
                {
                    Console.WriteLine("电脑出" + c);
                    Console.WriteLine("平局");
                }
                else
                {
                    Console.WriteLine("输入错误!请按规矩出牌");
                } 
            }
           


        }

 

6.做一个算缘分的小游戏:

输入男方姓名,女方姓名,输出缘分指数,给出建议。

 static void Main(string[] args)
        {
            Console.Write("男方姓名:");
            string nan = Console.ReadLine();
            Console.Write("女方姓名:");
            string nv = Console.ReadLine();

            Random rand = new Random();
            int n = rand.Next(100);
            n++;

            string jianYi = "";
            if (n > 0 && n < 30)
            {
                jianYi = "分手吧";
            }
            else if (n >= 30 && n < 60)
            {
                jianYi = "一起努力";
            }
            else if (n >= 60 && n <= 80)
            {
                jianYi = "幸福一对";
            }
            else 
            {
                jianYi = "鸳鸯配";
            }

            Console.WriteLine("{0}和{1}的缘分指数是:{2}。建议:{3}", nan, nv, n,jianYi);
        }

7.判断输入的两位数是否与7相关

 static void Main(string[] args)
        {
            //判断输入的两位数是否与7相关
           
            Console.WriteLine("请输入一个两位数:");
            int a = Convert.ToInt32(Console.ReadLine());
            
            if (a%7==0||a%10==7 || a / 10 == 7)
            {
                Console.WriteLine("这个数与7相关。");
            }
            else
            {
                Console.WriteLine("这个数与7无关。");
            }
            
        }

8.你能跑过豹子吗?

 static void Main(string[] args)
        {
            //你能跑过豹子吗?

            Console.WriteLine("你能跑过豹子吗?(Y/N)");
            string s = Console.ReadLine();

            if (s=="y"||s=="Y")
            {
                Console.WriteLine("你比禽兽还禽兽!");
            }
            else
            {
                Console.WriteLine("你禽兽不如。");
            }
        }

9.判断输入的年份是闰年,还是平年

static void Main(string[] args)
        {
            //判断输入的年份是闰年,还是平年

            Console.Write("请输入年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            //1.被400整除; 2.被4整除,但不能被100整除
            if (year%400==0||(year%100!=0&&year%4==0))
            {
                Console.WriteLine("是闰年");
            }
            else
            {
                Console.WriteLine("是平年");
            }
        }

10.输入三个数,三个数中找出最大的来

 static void Main(string[] args)
        {
            //输入三个数,三个数中找出最大的来
            Console.WriteLine("请输入三个数:");
            int a, b, c;
            int max = 0;
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());

            if (a>b)
            {
                max = a;
            }
            else
            {
                max = b;
            }
            if (max<c)
            {
                max = c;
            }
            Console.WriteLine("最大数是"+max);
        }

11.输入年龄,判断是童年,少年,青年,中年,老年。

 static void Main(string[] args) 
        {
            //输入年龄,判断是童年,少年,青年,中年,老年。
            Console.WriteLine("请输入年龄:");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age>0&&age<=6)
            {
                Console.WriteLine("童年");
            }
            else if (age>6&&age<16)
            {
                Console.WriteLine("少年");
            }
            else if (age>=16&&age<=30)
            {
                Console.WriteLine("青年");

            }
            else if (age>30&&age<60)
            {
                Console.WriteLine("中年");
            }
            else if (age>=60&&age<120)
            {
                Console.WriteLine("老年");
            }
            else
            {
                Console.WriteLine("输入错误");
            }
        }

 

转载于:https://www.cnblogs.com/kellybutterfly/p/5389225.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SAS提供了多种类型的判断语句,包括if语句和条件语句。if语句由条件组成,如果条件为真,则执行特定的操作。if语句后面可以跟随else语句,当布尔条件为假时执行相应的操作。此外,if语句还可以嵌套使用,即在if语句后面再跟一对if-then语句。在SAS中,if语句可以用于删除特定的观察值。例如,如果条件为真,则从数据集中删除特定的数据。 SAS的条件语句if-then的基本形式是:if 条件 then 执行;例如,如果年份小于2000,则执行删除操作。条件语句if-then可用于根据给定的条件执行单条语句。 if语句是用来进行条件判断的,其使用格式如下:if 要判断的条件:条件成立时要执行的操作;满足条件时执行的操作;不满足条件时执行的操作。if语句可以根据条件的不同执行不同的操作。在SAS中,通过if条件语句,可以根据具体的条件进行相应的数据处理和操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SAS IF判断语句](https://blog.csdn.net/lvchunyang66/article/details/85773029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [SAS 条件语句](https://blog.csdn.net/m0_51011378/article/details/121053381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [判断语句和循环语句(基础篇三)](https://download.csdn.net/download/weixin_38621624/14856899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值