【Language】C#之旅_③_條件陳述式

來源:互聯網
上載者:User

namespace ConsoleApplication1
{
    class Program
    {
        enum hobby { reading,writing,singing,dancing }
        static void Main(string[] args)
        {
                     /*條件陳述式*/
            int month;
            month=0;
            /*if 語句*/
                //①文法—if(布爾值) 程式語句
                if (month < 1)
                {
                     Console.WriteLine("月份不存在!");
                }
                //②文法—if(布爾值) 程式語句a else 程式語句b
                month++;
                if (month == 1)
                {
                    Console.WriteLine("現在是一月");
                    month++;
                }
                else
                {
                    Console.WriteLine("月份不存在!");
                }
                //③文法—if(布爾值) 程式語句a else if 程式語句b else if 程式語句c … …
             
                if (month == 1)
                {
                    Console.WriteLine("現在是一月");
                }
                else if (month == 2)
                {
                    Console.WriteLine("現在是二月");
                }
                else if (month == 3)
                {
                    Console.WriteLine("現在是三月");
                }
                else
                {
                    Console.WriteLine("月份不存在!");
                }
                //當然還可以同時使用多個布爾值或者是上一小節的三元運算子(?:)

            /*switch 語句*/
                //文法:
                //switch (參數)
                //  {
                //      case 對應的參數值a:操作a;break;
                //      case 對應的參數值b:操作b;break;
                //      default:預設操作c;break;
                //  }
                int x=0;
                switch (x)
                {
                    case 0: Console.WriteLine("x的值是:0"); break;
                    case 1: Console.WriteLine("x的值是:1"); break;
                    default: Console.WriteLine("預設執行該情況"); break;
                }

                hobby myHobby = hobby.dancing;//枚舉類型的使用——沒忘了在開頭定義的枚舉類型的參數hobby吧?
                switch (myHobby)
                {
                    case hobby.dancing: Console.WriteLine("我喜歡的是跳舞吖!"); break;
                    case hobby.reading: Console.WriteLine("我喜歡的是讀書啊!"); break;
                    case hobby.singing: Console.WriteLine("我喜歡的是唱歌啊!"); break;
                    case hobby.writing: Console.WriteLine("我喜歡的是寫作哦!"); break;
                    default: Console.WriteLine("這些都不是我的愛好!"); break;
                }
               
                switch (myHobby)    //組合case的使用
                {
                    case hobby.dancing:
                    case hobby.reading: Console.WriteLine("我喜歡的是讀書and跳舞啊!"); break;
                    case hobby.singing:
                    case hobby.writing: Console.WriteLine("我喜歡的是寫作and唱歌哦!"); break;
                    default: Console.WriteLine("這些都不是我的愛好!"); break;
                }
  
            Console.ReadKey();//等待user輸入
        }
    }
}

聯繫我們

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