C# 用户输入引用枚举
这是我的作业:在您的程序中,要求用户输入月份名称。您的程序将输出该月的相应值。
所需输出的示例:
Enter the name of a month: April
April is month 4
这是我当前的代码。我不是程序员,而且我以某种方式颠倒了练习的要点:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Enter the name of a month: ");
String month1 = System.Console.ReadLine();
Months month = (Months)Convert.ToInt32(month1);
System.Console.WriteLine(month);
}
}
enum Months
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12,
}
我的输出需要月份数,然后给出实际月份。 谢谢。
Here is my assignment: In your program, ask the user to enter the name of a month. Your program will output the corresponding value for that month.
Example of desired output:
Enter the name of a month: April
April is month 4
Here is my current code. I'm not a programmer, and I've somehow kind of reversed the point of the exercise:
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Enter the name of a month: ");
String month1 = System.Console.ReadLine();
Months month = (Months)Convert.ToInt32(month1);
System.Console.WriteLine(month);
}
}
enum Months
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12,
}
The output of mine requires the number of the month, then gives the actual month.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 Enum.Parse 方法。
备注部分中的示例甚至显示了如何处理未定义的值:
Have a look at Enum.Parse Method.
The exmple in the Remarks section even shows how to handle values that are not defined:
也许考虑完全避免枚举:
Perhaps consider avoiding enums entirely: