首页 > 代码库 > if语句

if语句

语句分类:

(1)分支语句

if,if  else,if  else if  else,switch  case

(2)循环语句

for, while, do while, for each

(3)跳转语句

break, continue

(4)异常语句

try catch finally

 

If语句:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsoleApplication2

    class Program

    {

        static void Main(string[] args)

        {

            //题目:控制台输入年份,判断是否为闰年

            Console.WriteLine("请输入一个年份");

            string bb = Console.ReadLine();

            int aa = int.Parse(bb);

            if((aa%400==0&&aa%100==0)||(aa%100!=0&&aa%4==0))

            {

                Console.WriteLine("是闰年");

            }

            else

            {

                Console.WriteLine("不是闰年");

            }

 

            //控制台分别录入年月日,判断是否是一个正确的日期

            Console.WriteLine("请输入一个年份");

            string year = Console.ReadLine();

            int y = int.Parse(year);

            Console.WriteLine("请输入一个月份");

            string month = Console.ReadLine();

            int m = int.Parse(month);

            Console.WriteLine("请输入一个日期");

            string day = Console.ReadLine();

            int d = int.Parse(day);

            if(y>0&&y<=9999)

            {

                if(m>0&&m<13)

                {

                    if(d>0&&d<32)

                    {

                        if (m == 2)

                        { 

                            if ((y % 100 == 0 && y % 400 == 0) || (y % 100 != 0 && y % 4 == 0))

                            {

                                if (d < 30)

                                {

                                    Console.WriteLine("日期输入正确");

                                }

                                else

                                {

                                    Console.WriteLine("日期输入错误");

                                }

                            }

                            else

                            {

                                if (d < 29)

                                {

                                    Console.WriteLine("日期输入正确");

                                }

                                else

                                {

                                    Console.WriteLine("日期输入错误");

                                }

                            }

                        }

                        else if(m==4||m==6||m==9||m==11)

                        {

                            if (d < 31)

                            {

                                Console.WriteLine("日期输入正确");

                            }

                            else

                            {

                                Console.WriteLine("日期输入错误");

                            }

                        }

                        else

                        {

                            if (d < 32)

                            {

                                Console.WriteLine("日期输入正确");

                            }

                            else

                            {

                                Console.WriteLine("日期输入错误");

                            }

                        }

                    }

                    else

                    {

                        Console.WriteLine("日期输入错误");

                    }

                }

                else

                {

                    Console.WriteLine("月份输入错误");

                }

            }

            else

            {

                Console.WriteLine("年份输入错误");

            }

 

            //输入一个1-100之内的数,判断是否跟7有关

            Console.WriteLine("请输入一个数:");

            string shuru = Console.ReadLine();

            int shu = int.Parse(shuru);

            if(shu%7==0||shu%10==7||shu/10==7)

            {

             Console.WriteLine("数字"+shu+"与7有关");

            }

            else

            {

                Console.WriteLine("数字"+shu+"与7无关");

            }

 

            //输入身高、体重、性别,判断是否是标准体重,男性标准=(身高-100)+-3,女性标准=(身高-110)+-3

            Console.WriteLine("请输入你的身高:");

            string shengao  =Console.ReadLine();

            int shg = int.Parse(shengao);

            Console.WriteLine("请输入你的体重:");

            string tizhong = Console.ReadLine();

            int tzh = int.Parse(tizhong);

            Console.WriteLine("请输入你的性别(男-1,女-0):");

            string xingbie = Console.ReadLine();

            int xb = int.Parse(xingbie);

            int bzh;

            if (xb == 1)

            {

               bzh=shg-100;

               if (tzh < bzh - 3)

               {

                   Console.WriteLine("您偏瘦");

               }

               else if (tzh > bzh + 3)

               {

                   Console.WriteLine("您偏胖");

               }

               else

               {

                   Console.WriteLine("您体重正常");

               }

            }

            else if (xb == 0)

            {

                bzh = shg - 110;

                if (tzh < bzh - 3)

                {

                    Console.WriteLine("您偏瘦");

                }

                else if (tzh > bzh + 3)

                {

                    Console.WriteLine("您偏胖");

                }

                else

                {

                    Console.WriteLine("您体重正常");

                }

            }

            else

            {

                Console.WriteLine("性别输入有误");

            }

 

            //输入一元二次方程的三个参数,a,b,c,判断是否为一元二次方程,并求解

            Console.WriteLine("请输入a的值");

            string a1 = Console.ReadLine();

            int a = int.Parse(a1);

            Console.WriteLine("请输入b的值");

            string b1 = Console.ReadLine();

            int b = int.Parse(b1);

            Console.WriteLine("请输入c的值");

            string c1 = Console.ReadLine();

            int c = int.Parse(c1);

 

            if (a == 0)

            {

                Console.WriteLine("因为a=0,所以这个方程不是一个一元二次方程");

            }

            else

            {

                decimal d1 = b * b - 4 * a * c;

                if(d1<0)

                {

                    Console.WriteLine("因为△<0,所以这个一元二次方程无解");

                }

                else if(d1==0)

                {

                    Console.WriteLine("因为△=0,所以这个一元二次方程有两个相等的实数根");

                    decimal x1 = -b / (2 * a);

                    Console.WriteLine("x1=x2="+x1);

                }

                else

                {

                    Console.WriteLine("因为△>0,所以这个一元二次方程有两个不相等的实数根");

                    decimal x1 = (-b + d1) / (2 * a);

                    decimal x2 = (-b - d1) / (2 * a);

                    Console.WriteLine("x1=" + x1);

                    Console.WriteLine("x2=" + x2);

                }

            }

            Console.ReadLine();

        }

    }

}

if语句