IF语句一 二 20140816

语句分类:

一.顺序

对程序逐一执行

二.分支

if语句--用来判断条件,bool型.

1.if()    2.if()    3.if()      4.if()

{      {      {        {

}      }      }        }

      else()    else if()      if()

                  {      {        {

       }      }        }

             else if()      else if()

              {        {

              }        }

              else()      else()

PS:if()后只能有一个else(),但可有多个else if().

例1:求根

static void Main5(string[] args)
        {
            Console.WriteLine("求一元二次方程:a*x*x+bx+c=0根的情况");
            Console.Write("a=");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.Write("b=");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.Write("c=");
            int c = Convert.ToInt32(Console.ReadLine());

            if (a == 0)
            {
                Console.WriteLine("不是一元二次方程");
            }
            else
            {
                Console.WriteLine("是一元二次方程");
                int delta = b * b - 4 * a * c;
                if (delta > 0)
                {
                   Console.WriteLine("x1={0},x2={1}", (-b + Math.Sqrt(delta) / 2 * a), (-b - Math.Sqrt(delta) / 2 * a));--开方,求根结果.
                }
                else if (delta == 0)
                {
                    Console.WriteLine("x=" + -b / 2 * a);
                }
                else
                {
                    Console.WriteLine("无实根");
                  
                 }

         }            

        
例2:猜拳

 static void Main(string[] args)
        {
            Console.Write("请出拳:");
            int human =Convert.ToInt32(Console.ReadLine());
            Random mm = new Random();
            int computer = mm.Next(3);


            if (human == 0)
            {
                if (computer == 0)
                {
                    Console.WriteLine("平局!");
                }
                if (computer == 1)
                {
                    Console.WriteLine("你出剪刀,电脑出石头,你输了!");
                }
                if (computer == 2)
                {
                    Console.WriteLine("你出剪刀,电脑出布,你赢了!");
                }
                else if (human == 1)
                {
                    if (computer == 0)
                    {
                        Console.WriteLine("你出石头,电脑出剪刀,你赢了!");
                    }
                    if (computer == 1)
                    {
                        Console.WriteLine("平局!");
                    }
                    if (computer == 2)
                    {
                        Console.WriteLine("你出石头,电脑出布,你输了!");
                    }
                    else if (human == 2)
                    {
                        if (computer == 0)
                        {
                            Console.WriteLine("你出布,电脑出剪刀,你输了!");
                        }
                        if (computer == 1)
                        {
                            Console.WriteLine("你出布,电脑出石头,你赢了!");
                        }
                        if (computer == 2)
                        {
                            Console.WriteLine("平局!");
                        }
                        else
                        {
                            Console.WriteLine("请按套路出拳!谢谢!");
                        }
                    }

                }

PS:生成随机生成器:

Random sss = new Random();

int 变量=sss.next();

Console.Writeline(变量);

例3:标准体重

static void Main8(string[] args)
        {
            Console.Write("性别:");
            string sex = Console.ReadLine();
            Console.Write("身高:");
            int h = Convert.ToInt32(Console.ReadLine());
            Console.Write("体重:");
            int w = Convert.ToInt32(Console.ReadLine());


            if (sex == "男")
            {
                int w1 = h - 100 - 3;
                int w2 = h - 100 + 3;

                if (w <= w2 && w >= w1)
                {
                    Console.WriteLine("体重正常!");
                }
                else if (w > w2)
                {
                    Console.WriteLine("体重偏胖哦,需要锻炼啦!");
                }
                else if (w < w1)
                {
                    Console.WriteLine("体重偏瘦,要注意营养搭配哦!");
                }
                else
                {
                    Console.WriteLine("亲有体重?");
                }
            }


            else
            {
                int w1 = h - 100 - 3;
                int w2 = h - 100 + 3;

                if (w <= w2 && w >= w1)
                {
                    Console.WriteLine("体重正常!");
                }
                else if (w > w2)
                {
                    Console.WriteLine("体重偏胖哦,需要锻炼啦!");
                }
                else if (w < w1)
                {
                    Console.WriteLine("体重偏瘦,要注意营养搭配哦!");
                }
                else
                {
                    Console.WriteLine("亲有体重?");
                }


            }
   PS:断点--检查程序错误

工具--选项--文本编辑器--所有语言--常规--行号(点选).--(CTRL+F5键不执行该程序,只能启动)

调试--启动调试--窗口--即时--逐语句.--看程序运行步骤,如执行“if”,即不执行“else”.

原文地址:https://www.cnblogs.com/DORCASQING/p/3918711.html