gpt4 book ai didi

c# - 在 Enum C# 中使用 Switch 语句

转载 作者:行者123 更新时间:2023-12-05 09:23:59 26 4
gpt4 key购买 nike

您好,我正在创建一个控制台应用程序,用户在其中输入月份的数字,该应用程序会计算出它是几月,并显示答案并显示该月的天数。当 switch 语句在 Main 方法中时我让它工作,但现在它显示错误的月份。我有以下代码:

namespace ConsoleCA2{

public enum Months
{
January = 31,
February = 28,
March = 31,
April = 30,
May = 31,
June = 30,
July = 31,
August = 31,
September = 30,
October = 31,
November = 30,
December = 31
};

public class Program
{
public const string NEWLINE = "\n"; //constant for new line


static void Main(string[] args)
{
try
{

//prompt user for month number.
Console.Write("Enter a Number to find out what Month it is: ");

//convert users answer to integer
int userInput = Convert.ToInt16(Console.ReadLine());

//declare and initalise
int daysInMonth = 0;
string answer = String.Empty;

//call relevent calculation methods
answer = DetermineMonth(userInput);
daysInMonth = DetermineDaysInMonth(userInput);

//Write users answer to screen
Console.WriteLine(answer);

//Prompt user, whether they want to know number of days in the
//month
Console.Write("Would you like to know the number of days in" +
" {0}: ", answer);

//assign users answer to variable
string userAnswer = Console.ReadLine();

//Output answer if needed.
if (userAnswer == "yes".Trim().ToLower())
{
Console.WriteLine("The number of days in {0} is: {1}",
answer, daysInMonth.ToString());
}
else if (userAnswer == "no".Trim().ToLower())
{
Console.WriteLine(NEWLINE);
Console.WriteLine("You answered No, Have a good day!");
}
else
{
Console.WriteLine("Please answer yes or no.");
}

Console.Read(); //Pauses screen for user
}
//catch any errors.
catch (Exception err)
{
Console.WriteLine("Error" + err.Message);
}

}


/// <summary>
/// Determines What month the use enters and returns string value
/// </summary>
/// <param name="userInput"></param>
/// <returns>returns the month as string</returns>
public static String DetermineMonth(int userInput)
{
switch (userInput)
{
case 1:
return Months.January.ToString();
case 2:
return Months.February.ToString();
case 3:
return Months.March.ToString();
case 4:
return Months.April.ToString();
case 5:
return Months.May.ToString();
case 6:
return Months.June.ToString();
case 7:
return Months.July.ToString();
case 8:
return Months.August.ToString();
case 9:
return Months.September.ToString();
case 10:
return Months.October.ToString();
case 11:
return Months.November.ToString();
case 12:
return Months.December.ToString();
default:
return "Error";
}



}

/// <summary>
/// Determines how many days is in selected month as integer
/// </summary>
/// <param name="userInput"></param>
/// <returns>returns how many days in month</returns>
public static int DetermineDaysInMonth(int userInput)
{
switch (userInput)
{
case 1:
return Convert.ToInt16(Months.January);
case 2:
return Convert.ToInt16(Months.February);
case 3:
return Convert.ToInt16(Months.March);
case 4:
return Convert.ToInt16(Months.April);
case 5:
return Convert.ToInt16(Months.May);
case 6:
return Convert.ToInt16(Months.June);
case 7:
return Convert.ToInt16(Months.July);
case 8:
return Convert.ToInt16(Months.August);
case 9:
return Convert.ToInt16(Months.September);
case 10:
return Convert.ToInt16(Months.October);
case 11:
return Convert.ToInt16(Months.November);
case 12:
return Convert.ToInt16(Months.December);
default:
return 0;
}
}

旧的 switch 语句是这样的:

        switch (userInput)
{
case 1:
answer = Months.January.ToString();
daysInMonth = Convert.ToInt16(Months.January);
break;
case 2:
answer = Months.February.ToString();
daysInMonth = Convert.ToInt16(Months.February);
break;
case 3:
answer = Months.March.ToString();
daysInMonth = Convert.ToInt16(Months.March);
break;
case 4:
answer = Months.April.ToString();
daysInMonth = Convert.ToInt16(Months.April);
break;
case 5:
answer = Months.May.ToString();
daysInMonth = Convert.ToInt16(Months.May);
break;
case 6:
answer = Months.June.ToString();
daysInMonth = Convert.ToInt16(Months.June);
break;
case 7:
answer = Months.July.ToString();
daysInMonth = Convert.ToInt16(Months.July);
break;
case 8:
answer = Months.August.ToString();
daysInMonth = Convert.ToInt16(Months.August);
break;
case 9:
answer = Months.September.ToString();
daysInMonth = Convert.ToInt16(Months.September);
break;
case 10:
answer = Months.October.ToString();
daysInMonth = Convert.ToInt16(Months.October);
break;
case 11:
answer = Months.November.ToString();
daysInMonth = Convert.ToInt16(Months.November);
break;
case 12:
answer = Months.December.ToString();
daysInMonth = Convert.ToInt16(Months.December);
break;
default:
answer = "Error";
break;
}

在我更改为单独的方法之前一直有效,然后它从那里开始走下坡路。现在即使我把它放回去,它仍然会混淆月份?

很困惑:S

预先感谢您的帮助...如果您能理解困惑的代码:p

最佳答案

NET Framework 包含大量代码。有时很难找到需要的东西,但已经有东西的可能性很高。而且,虽然搜索需要时间,但这是一个值得的时间,因为您可以为自己提供对 future 工作有用的知识

CultureInfo.CurrentCulture.DayFormatInfo.MonthNames
DateTime.DaysInMonth

using System.Globalization;
static void Main(string[] args)
{
Console.Write("Enter a Number to find out what Month it is: ");

int userInput = Convert.ToInt16(Console.ReadLine());
if(userInput > 0 && userInput < 13)
{
string monthName = CultureInfo.CurrentCulture.DayTimeFormat.MonthNames[userInput-1];
int daysInMonth = DateTime.DaysInMonth(2013, userInput);
......

}
}

如果您还需要考虑闰年,您应该获得年份的输入,但这非常简单

关于c# - 在 Enum C# 中使用 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com