c# switch 语句_C#程序演示switch语句的示例

c# switch 语句

A switch statement allows checking a variable/value with a list of values (cases) and executing the block associated with that case.

switch语句允许使用值(案例)列表检查变量/值,并执行与该案例关联的块。

Syntax:

句法:

    switch(variable/expression)
    {
	    case <case_value1>:
		    statement(s);
		    break;
	    case <case_value2>:
		    break;
	    default:
		    break;
    }

Note:

注意:

  • default case is an optional - but it should be used, because no case value is matched then default statements will be printed.

    default case是可选的-但应使用它,因为没有case值匹配,则将打印默认语句。

  • “break” is an optional but if we are testing multiple case values together. Like,

    “ break”是可选的,但是如果我们要同时测试多个case值。 喜欢,

        case <case_value1>:
        case <case_value2>:
            statement(s);
            break;
    
    

C#代码演示switch语句示例 (C# code to demonstrate example of switch statement)

Here, we are asking for a day number from the user between the range of 0 to 6, and printing day name (example: 0 for Sunday, 1 for Monday and so on...).

在这里,我们要求用户在0到6之间的日期编号,并打印日期名称(例如:0代表星期日,1代表星期一,依此类推...)。

// C# program to demonstrate example of switch statement
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int day;
            
            //input the day number
            Console.Write("Enter day number (0-6): ");
            day = Convert.ToInt32(Console.ReadLine());
            
            //checking 
            switch (day)
            {
                case 0:
                    Console.WriteLine("Sunday");
                    break;
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                case 6:
                    Console.WriteLine("Saturday");
                    break;

                default:
                    Console.WriteLine("Invalid Input");
                    break;
            }

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

First run:
Enter day number (0-6): 3
Wednesday

Second run:
Enter day number (0-6): 9
Invalid Input

使用switch语句检查输入字符是VOWEL还是CONSOTANT的C#代码 (C# code to check input character is a VOWEL or CONSOTANT using switch statement )

Here, we are asking for a character from the user – and checking 1) input character is an alphabet using if-else statement and if it is an alphabet – we are checking for vowels & consonants.

在这里,我们要求用户输入一个字符-并检查1)输入的字符是否使用if-else语句输入字母,如果输入的是字母,我们正在检查元音和辅音。

// C# program to demonstrate example of switch statement
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            
            //input a character
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];
            
            //checking for valid alphabet
            if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
            {
                //checking for vowels
                switch (ch)
                {
                    case 'A':
                    case 'a':
                    case 'E':
                    case 'e':
                    case 'I':
                    case 'i':
                    case 'O':
                    case 'o':
                    case 'U':
                    case 'u':
                        Console.WriteLine("Input character {0} is a Vowel", ch);
                        break;

                    default:
                        Console.WriteLine("Input character {0} is a Consonat", ch);
                        break;
                }
            }
            else
            {
                Console.WriteLine("Input character {0} is not a valid alphabet", ch);
            }


            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

First run:
Enter a character: X
Input character X is a Consonat

Second run:
Enter a character: i
Input character i is a Vowel

Third run:
Enter a character: $
Input character $ is not a valid alphabet


翻译自: https://www.includehelp.com/dot-net/switch-statement-example-in-c-sharp.aspx

c# switch 语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值