首页 > 代码库 > 编程中的 if ()else() 语句

编程中的 if ()else() 语句

例句    if()  else()

            double d = Convert.ToDouble(Console .ReadLine());

            if (d >= 60 && d<=100)

            {

                Console.WriteLine("及格");

            }

            else

            {

                Console.WriteLine("不及格");

            }

            Console.ReadLine();

 

例题

1·将一个浮点数四舍五入为整数。

 

            Console.Write("请输入:");

            double d = Convert.ToDouble(Console.ReadLine());

            if (d-(int)d>=0.5)

            {

                d = (int)d;

                d++;

            }

            else

            {

                d=(int)d;

            }

            Console.WriteLine("结果为:"+d.ToString());

            Console.ReadLine();

 

 

 

2·求一元二次方程的根(a*x*x+b*x+c=0

 

            Console.WriteLine("一元二次方程为:a*x*x+b*x+c=0,求方程的根");

            Console.Write("请输入:a=");

            double a = Convert.ToDouble(Console.ReadLine());

            Console.Write("请输入:b=");

            double b = Convert.ToDouble(Console.ReadLine());

            Console.Write("请输入:c=");

            double c = Convert.ToDouble(Console.ReadLine());

 

            if (a == 0)

            {

                Console.WriteLine("这不是一元二次方程!!!");

            }

            else

            {

                double dr = b * b - 4 * a * c;

                if (dr > 0)

                {

                    double x1 = (-b + Math.Sqrt(dr)) / 2 * a;

                    double x2 = (-b - Math.Sqrt(dr)) / 2 * a;

                    int c1 = (int)x1;

                    int c2 = (int)x2;

                    Console.WriteLine("有两个不相等的根, x1={0}x2={1}", c1, c2);

                }

                if (dr == 0)

                {

                    double x = (-b) / 2 * a;

                    int c3=(int)x;

                    Console.WriteLine("有一个根,x=" + c3);

                }

                if (dr < 0)

                {

                    Console.WriteLine("无实根!");

                }

            }

            Console.ReadLine();

 

 

 

3·判断体重是否标准(男士标准体重=身高-100±3;女士标准体重=身高-110±3

 

            Console.WriteLine("体重检测程序");

            Console.Write("请输入性别:");

            string x = Convert.ToString(Console.ReadLine());

            Console.Write("请输入体重(kg)");

            double t = Convert.ToDouble(Console.ReadLine());

            Console.Write("请输入身高(cm)");

            double s = Convert.ToDouble(Console.ReadLine());

 

            if (x == "" || x == "")

            {

                if (x == "")

                {

                    double bzt1 = s - 100 + 3;                // bzt1为标准体重

                    double bzt2 = s - 100 - 3;                // bzt2为标准体重

                    if (t > bzt1)

                    {

                        Console.WriteLine("游泳可以不用救生圈了");

                    }

                    else if (t <= bzt1 && t >= bzt2)

                    {

                        Console.WriteLine("身材不错,继续保持");

                    }

                    else

                    {

                        Console.WriteLine("亲~以后多吃点");

                    }

                }

                else

                {

                    double bzt1 = s - 110 + 3;

                    double bzt2 = s - 110 - 3;

                    if (t > bzt1)

                    {

                        Console.WriteLine("游泳可以不用救生圈了");

                    }

                    else if (t <= bzt1 && t >= bzt2)

                    {

                        Console.WriteLine("身材不错,继续保持");

                    }

                    else

                    {

                        Console.WriteLine("亲~以后多吃点");

                    }

                }

            }

            else

            {

               Console.WriteLine("朋友,性别都不知道了?");

            }

            Console.ReadLine();

 

编程中的 if ()else() 语句