2

您好我正在创建一个控制台应用程序,用户在其中输入月份的数字,该应用程序会计算出月份并显示答案并显示该月的天数。当 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

4

6 回答 6

6

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

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);
         ......

    }
}

如果你还需要考虑闰年,你应该得到这一年的输入,但这很简单

于 2013-07-20T09:56:17.303 回答
1

您可以使用 anenum为常量创建符号名称。在这种情况下,这不是你想要的。

我认为你想要的是一个查找表,给定一个值 A,返回一个值 B。在你的情况下,你可以使用几个数组:

string[] monthNames = new string[]
{
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "November", "December"
};
int[] monthDays = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

然后您可以通过查找获得月份名称和天数:

int userInput = Convert.ToInt16(Console.ReadLine());
if (userInput < 1 || userInput > 12)
    Console.WriteLine("Error: invalid month.");
else
{
    answer = monthNames[userInput-1];
    daysInMonth = monthDays[userInput-1];
}

还有其他方法可以做到这一点,包括在几个月和几天内使用结构或类,但这些都是更高级的主题。

此外,.NET 有一个DateTime 结构,它具有所有这些功能以及更多用于处理日期和时间的功能。在生产程序中,您将使用它而不是自己滚动。

于 2013-07-20T09:39:55.017 回答
1

那是因为你的枚举把这一切都搞混了。

31 和 30 有重复值。

这是一个工作示例(但是有更多优化它的方法。比如使用DateTime类)

public enum Months : int
{
    None = 0,
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
};

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;
            Months answer;

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

            if (answer == Months.None) 
            {
                Console.WriteLine("Please enter a value between 1 and 12");
                return;
            }

            daysInMonth = DetermineDaysInMonth(answer);

            //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.ReadLine(); //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 Months DetermineMonth(int userInput)
    {
        switch (userInput)
        {
            case 1:
                return Months.January;
            case 2:
                return Months.February;
            case 3:
                return Months.March;
            case 4:
                return Months.April;
            case 5:
                return Months.May;
            case 6:
                return Months.June;
            case 7:
                return Months.July;
            case 8:
                return Months.August;
            case 9:
                return Months.September;
            case 10:
                return Months.October;
            case 11:
                return Months.November;
            case 12:
                return Months.December;
        }
        return Months.None;
    }

    /// <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(Months userInput)
    {
        switch (Convert.ToInt32(userInput))
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 2:
                return 28;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
        }
        return 0;
    }
}
于 2013-07-20T09:28:46.930 回答
0

这不是您使用枚举的方式。

它们不仅仅是静态键值对:如果它们是,您可以使用的不仅仅是整数类型。不,它们只不过是整数类型的类型安全包装器。

你的问题是这样的:

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
};

这意味着,据 CLR 所知,1 月、3 月、5 月、7 月、8 月、10 月和 12 月是完全相同的。与 4 月、6 月、9 月和 11 月的交易相同。

正确的方法是这样的:

public enum Months
{
    None = -1,
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
};

然后你的DetermineMonth方法就是这样:

public static Months DetermineMonth(int userInput)
{
    Months temp = (Months)userInput;
    return Enum.IsDefined(typeof(Months), temp) ? temp : Months.None; 
}

然后DetermineDaysInMonth会是这样的:

public static int DetermineDaysInMonth(int userInput)
{
    if (!((Months[])Enum.GetValues(typeof(Months))).Contains((Months)userInput)
        return 0;
    else return DateTime.DaysInMonth(DateTime.Today.Year, userInput);
}
于 2013-07-20T09:48:56.443 回答
0

您可以在返回天数时使用Extension 方法-enum

public enum Months
{
    January,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
}

public static class MonthsExtensions
{
    public static int DaysInMonths(this Months month)
    {
        switch (month)
        {
            case Months.January:
            case Months.March:
            case Months.May:
            case Months.July:
            case Months.August:
            case Months.October:
            case Months.December:
                return 31;

            case Months.April:
            case Months.June:
            case Months.September:
            case Months.November:
                return 30;

            case Months.February:
                return 28;
        }
        return 0;
    }

然后你可以得到这样的天数 -

int daysInMonths = Months.January.DaysInMonths();
于 2013-07-20T09:49:40.607 回答
0

您可能需要考虑稍微简化一下,并使您的枚举数字与月份相匹配。然后将该月的日子存储在其他地方。

然后您可以执行 (Months)userInput 来获取枚举并消除至少一个 switch 语句。

您可以使用字典或其他方式存储每个月的日期,只要枚举为每个值存储一个唯一的 int 即可。

于 2013-07-20T09:40:36.203 回答