C#case语句实例
时间: 2023-07-27 20:18:48 浏览: 96
以下是一个使用C# case语句的实例,用于根据用户输入的数字输出对应的星期几:
```
int day = 3;
string dayString;
switch(day) {
case 1:
dayString = "Monday";
break;
case 2:
dayString = "Tuesday";
break;
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
dayString = "Invalid day";
break;
}
Console.WriteLine("Today is " + dayString);
```
在上面的例子中,用户输入的数字为3,程序将执行第三个case,将dayString赋值为"Wednesday",然后输出"Today is Wednesday"。如果用户输入的数字不在1到7之间,则执行default语句,将dayString赋值为"Invalid day",然后输出"Today is Invalid day"。
阅读全文