switch case ,while, do while,enum

來源:互聯網
上載者:User

標籤:switch case   while   do while   enme   

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace C_編輯基礎{     //枚舉的意義就是限定變數的取值範圍    enum gender{Male,Female,Unkown}; //聲明一個類型,它是枚舉類型(定義一個枚舉),這個枚舉有三個值。Male,Female,Unkown    class Program    {        static void Main(string[] args)        {            //=======================================================switch case語句                        //switch case被判定的值進入滿足條件的分支執行,主要是用於一些離散的值進行判斷。它只能判斷一下這個i,如果i=100,執行這段代碼,如果i=20,執行那段代碼,等等,做這樣的“=”判斷。像i>100 怎麼樣,i<20又怎麼樣..等這樣的地區條件判斷switch case是很難做的。注意語句結束之後一定要有“break”            //int i = 8;//可以先給i一個預設值,也可以不用:比如            int i =Convert.ToInt32( Console.ReadLine());            switch (i)            {                case 2: //相當於if(i==2)                    Console.WriteLine("這裡是中國");                    break;                case 4:                    Console.WriteLine("這裡是美國");                    break;                case 6:                    Console.WriteLine("這裡是歐洲");                    break;                case 8:                case 10:                case 12:   //相當於if(i==8||i==10||i==12) 這是唯一一種case後面不用寫break的情況,其他都要寫break                    Console.WriteLine("大哥,你來自火星");                    break;                default:  //相當於if語句的else                    Console.WriteLine("我真不知道你來自哪兒!");                    break;                           }            Console.ReadKey();                        //============================================================while語句            /*            int i=0;            int sum = 0;            while (i < 100) //如果i<100 就輸出i從0到100的累加和            {                i++;                sum = sum + i;                            }            Console.WriteLine(sum);            Console.ReadKey();            */            /*            while (true) //如果括弧裡一直是true的話,大括弧裡的代碼就會一直無止境的執行下去。            {                Console.WriteLine("我在運行,死迴圈的運行");            }            */            //當使用者輸入的密碼錯誤的時候就一直提示"密碼錯誤,請重新輸入"            /*            Console.WriteLine("請輸入密碼");            while (true)            {                                string password = Console.ReadLine();                if (password != "andmin")                {                    Console.WriteLine("密碼錯誤,請重新輸入");                                    }                else                {                    Console.WriteLine("密碼正確");                    break;                }                            }            Console.ReadKey();                           */            /*            //讓使用者不停得輸入"數字" 當使用者輸入"end"的時候結束,列印出使用者剛剛輸入的最大數字。            int max = 0;                        while (true)            {                Console.WriteLine("請輸入一個數");                string number = Console.ReadLine();                if (number == "end")                {                    Console.WriteLine("你剛剛輸入的最大值是{0}", max);                    Console.ReadKey();                    return;                }                int numberint = Convert.ToInt32(number);                if (numberint > max)                {                    max = numberint;                }                           }            */            //============================================================do while語句            /*            int i=0;            do            {                i++;                Console.WriteLine(i);            }            while (i < -10);   //不管條件成不成立,都先執行上面的大括弧裡面的內容一次            Console.ReadKey();            */            //=================================================枚舉的使用            gender a = gender.Female; //取值        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.