C#语法基础05_switch

C#语法基础05_switch


switch(expression){
    case constant-expression  :
       statement(s);
       break; 
    case constant-expression  :
       statement(s);
       break; 
  
    /* 您可以有任意数量的 case 语句 */
    default : /* 可选的 */
       statement(s);
       break; 
}

例子,通过switch实现的温度转化方式的选择:

class Program {
        static void Main(string[] args) {
            int choice = '0';
            bool loopFlag = true;
            bool firstLoop = true;

            PrintMsg();
            choice = GetChoice();

            while (loopFlag) {
                if (firstLoop) {
                    firstLoop = false;
                } else {
                    Console.WriteLine("Choose the conversion mode again\n");
                    choice = GetChoice();
                }
                switch (choice) {
                    case 1: _1_C2F(); break;
                    case 2: _2_F2C(); break;
                    case 3: _3_C2K(); break;
                    case 4: _4_K2C(); break;
                    case 5: _5_exit(); loopFlag = false; break;
                    default: break;
                }
            }
        } // end "main"

        static void PrintMsg() {
            Console.WriteLine("Please choose from one of the following options");
            Console.WriteLine("1. Convert Celsius to Fahrenheit");
            Console.WriteLine("2. Convert Fahrenheit to Celsius");
            Console.WriteLine("3. Convert Celsius to Kelvin");
            Console.WriteLine("4. Convert Kelvin to Celsius");
            Console.WriteLine("5. Exit this program");
            Console.WriteLine("\nEnter your option from 1 to 5\n");
        }

        static int GetChoice() {
            int result = int.Parse(Console.ReadLine());
            return result;
        }
        static double GetDouble() {
            double result = double.Parse(Console.ReadLine());
            return result;
        }

        static void _1_C2F() {
            Console.WriteLine("Enter the Celsius");
            double Celsius = GetDouble();
            double Fahrenheit = (Celsius * 1.8) + 32;
            Console.WriteLine("The corresponding Fahrenheit is {0:F2}", Fahrenheit);
        }

        static void _2_F2C() {
            Console.WriteLine("Enter the Fahrenheit");
            double Fahrenheit = GetDouble();
            double Celsius = (Fahrenheit - 32) / 1.8;
            Console.WriteLine("The corresponding Celsius is {0:F2}", Celsius);
        }

        static void _3_C2K() {
            Console.WriteLine("Enter the Celsius");
            double Celsius = GetDouble();
            double Kelvin = Celsius + 273.15;
            Console.WriteLine("The corresponding Celsius is {0:F2}", Kelvin);

        }

        static void _4_K2C() {
            Console.WriteLine("Enter the Kelvin");
            double Kelvin = GetDouble();
            double Celsius = Kelvin - 273.15;
            Console.WriteLine("The corresponding Celsius is {0:F2}", Celsius);
        }

        static void _5_exit() {
            Console.WriteLine("You have exit the program");

        }


    }
posted on 2021-04-06 15:47  摸鱼time  阅读(76)  评论(0编辑  收藏  举报