.NET基礎--if-else與swirch的區別

<span style="white-space:pre">	</span>    int score = 91;
            if (score >= 90 && score <= 100)
            {
                Console.WriteLine("優秀");
            }
            else if (score >= 80 && score < 90)
            {
                Console.WriteLine("良好");
            }
            else if (score >= 60 && score < 80)
            {
                Console.WriteLine("及格");
            }
            else if (score >= 0 && score < 60)
            {
                Console.WriteLine("不及格");
            }
            else
            {
                Console.WriteLine("請輸入0~100的成績.");
            }
            int i = score/10;
            switch (i)
            {
                case 10:
                    
                case 9:
                    Console.WriteLine("優秀");
                    break;
                case 8:
                    Console.WriteLine("良好");
                    break;
                case 7:
                case 6:
                    Console.WriteLine("及格");
                    break;
                case 5:
                case 4:
                case 3:
                case 2:
                case 1:
                case 0:
                    Console.WriteLine("不及格");
                    break;  
                default:
                    Console.WriteLine("請輸入0~100的成績.");
                    break;
            }
            Console.ReadLine();


輸出結果都是:優秀

if-else:主要用於數據大的範圍。

switch:主要用於比較精確的範圍。而且不僅僅侷限於可運算的數據類型。還可以運用於字符串比較。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章