C#程序从给定的工作日编号(0-6)打印工作日名称。 switch语句允许使用值列表或案例列表检查值。
工作日数字是其值从0到6的数字。 0表示“星期日” ,1表示“星期一” ,2表示“星期二” ,3表示“星期三” ,4表示“星期四” ,5表示“星期五” ,6表示“星期六” 。这将使用switch语句进行检查。
例子:
Input: 2
Output: Tuesday
Input: 6
Output: Saturday
范例1:
// C# program to input weekday number
// print the weekday
using System;
using System.IO;
using System.Text;
namespace Geeks
{
class GFG
{
// Main Method
static void Main(string[] args)
{
int weekday;
// input weekday number
Console.Write("Enter weekday number (0-6): ");
weekday = Convert.ToInt32(Console.ReadLine());
// Using switch case to validate
switch (weekday)
{
case 0:
Console.WriteLine("It is SUNDAY");
break;
case 1:
Console.WriteLine("It is MONDAY");
break;
case 2:
Console.WriteLine("It is TUESDAY");
break;
case 3:
Console.WriteLine("It is WEDNESDAY");
break;
case 4:
Console.WriteLine("It is THURSDAY");
break;
case 5:
Console.WriteLine("It is FRIDAY");
break;
case 6:
Console.WriteLine("It is SATURDAY");
break;
}
}
}
}
输出:当我们输入介于(0-6)之间的数字时。
Enter weekday number (0-6): It is WEDNESDAY
范例2:
// C# program to input weekday number
// print the weekday
using System;
using System.IO;
using System.Text;
namespace Geeks
{
class GFG
{
// Main Method
static void Main(string[] args)
{
int weekday;
// input weekday number
Console.Write("Enter weekday number (0-6): ");
weekday = Convert.ToInt32(Console.ReadLine());
// Using switch case to validate
switch (weekday)
{
case 0:
Console.WriteLine("It is SUNDAY");
break;
case 1:
Console.WriteLine("It is MONDAY");
break;
case 2:
Console.WriteLine("It is TUESDAY");
break;
case 3:
Console.WriteLine("It is WEDNESDAY");
break;
case 4:
Console.WriteLine("It is THURSDAY");
break;
case 5:
Console.WriteLine("It is FRIDAY");
break;
case 6:
Console.WriteLine("It is SATURDAY");
break;
// if no case value is matched
default:
Console.WriteLine("It is wrong input");
break;
}
}
}
}
输出:当我们输入的数字超出(0-6)范围时。
Enter weekday number (0-6): It is wrong input