IF語句 20140816

來源:互聯網
上載者:User

標籤:strong   ar   2014   amp   line   new   ad   on   

語句分類:

一.順序

對程式逐一執行

二.分支

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”.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.