c#读取月份的数字并输出月份的名称

本文关键字:输出 读取 取月份 数字 | 更新日期: 2023-09-27 18:15:07

我试着写一个c#控制台程序来读取一个月的数字并输出该月的名称,然后询问用户是否想知道该月的天数,如果是,输出天数。假设没有闰年,二月总是只有28天。

如果有人可以帮助,提前感谢!!

编辑:

这就是我到目前为止所拥有的,我在问题的后半部分遇到了麻烦,我不确定如何询问用户是否想知道一个月的天数以及如何使用开关输出天数…

class MainClass
{
  public static void Main (string[] args)
  {
    {
      Console.WriteLine("Give me an integer between 1 and 12, and I will give you the month");
      int monthInteger = int.Parse(Console.ReadLine());
      DateTime newDate = new DateTime(DateTime.Now.Year, monthInteger, 1);
      Console.WriteLine("The month is: " + newDate.ToString("MMMM"));
      Console.WriteLine();

c#读取月份的数字并输出月份的名称

一个简单的开关箱就可以了?

string input = Console.In.ReadLine();
int number = -1;
int.TryParse(input, out number);
switch (number)
{
    case 1:
        Console.Out.WriteLine("January");
        break;
    case 2:
        Console.Out.WriteLine("February");
        break;
    case -1:
        Console.Out.WriteLine("Please input a valid number");
        break;
    default:
        Console.Out.WriteLine("There are only 12 months in a year");
        break;
}

我想这足以完成你剩下的代码了。

下次,请提供一些你已经尝试过的代码,仅仅要求简单的代码通常是行不通的。

public static string getName(int i)
{
    string[] names = { "jan", "feb", ... } // fill in the names
    return names[i-1];
}
public static string getMonthName(int mounth)
{
    DateTime dt = new DateTime(2000, mounth, 1);
    return dt.ToString("M").Substring(0, dt.ToString("M").IndexOf(' '));
}

根据您的其他相关问题,作为这个问题的副本关闭(https://stackoverflow.com/questions/24996241/c-sharp-number-of-days-in-a-month-using-a-switch#24996339)…

这显然是一个学术练习,希望您了解switch语句。

下面是一个完整的示例,演示了执行switch语句的几种方法。由于您已经从用户那里获取了月份号,因此可以通过创建月份和该月中的天数之间的映射来打开该值。

即:

class MainClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Give me an integer between 1 and 12, and I will give you the month");
        int monthInteger = int.Parse(Console.ReadLine()); // WARNING: throws exception for non-integer input
        Console.WriteLine(GetMonthName(monthInteger));
        Console.WriteLine();
        Console.Write("Display days in month (y/n)? ");
        if (Console.ReadLine() == "y")
        {
            int daysInMonth = GetDaysInMonth_NoLeapYear(monthInteger);
            if (daysInMonth > 0)
            {
                Console.WriteLine(String.Format("{0} days in {1}",
                    daysInMonth.ToString(),
                    GetMonthName(monthInteger)));
            }
            else
            {
                Console.WriteLine("Invalid month entered.");
            }
            Console.WriteLine();
        }
        Console.WriteLine("Hit enter to close");
        Console.ReadLine();
    }
    private static String GetMonthName(int monthInteger)
    {
        DateTime newDate = new DateTime(DateTime.Now.Year, monthInteger, 1);
        String monthName = newDate.ToString("MMMM");
        return monthName;
    }
    /// <summary>
    /// Prints days in month. Assumes no leap year (since no year context provided) so Feb is always 28 days.
    /// </summary>
    /// <param name="monthInteger"></param>
    private static int GetDaysInMonth_NoLeapYear(int monthInteger)
    {
        int daysInMonth = -1; // -1 indicates unknown / bad value
        switch (monthInteger)
        {
            case 1: // jan
                daysInMonth = 30;
                break;
            case 2: // feb
                daysInMonth = 28; // if leap year it would be 29, but no way of indicating leap year per problem constraints
                break;
            case 3: // mar
                daysInMonth = 31;
                break;
            case 4: // apr
                daysInMonth = 30;
                break;
            case 5: // may
                daysInMonth = 31;
                break;
            case 6: // jun
                daysInMonth = 30;
                break;
            case 7: // jul
                daysInMonth = 31;
                break;
            case 8: // aug
                daysInMonth = 31;
                break;
            case 9: // sep
                daysInMonth = 30;
                break;
            case 10: // oct
                daysInMonth = 31;
                break;
            case 11: // nov
                daysInMonth = 30;
                break;
            case 12: // dec
                daysInMonth = 31;
                break;
        }
        return daysInMonth;
    }
    /// <summary>
    /// Prints days in month. Assumes no leap year (since no year context provided) so Feb is always 28 days.
    /// </summary>
    /// <param name="monthInteger"></param>
    private static int GetDaysInMonth_NoLeapYear_Compact(int monthInteger)
    {
        // uses case statement fall-through to avoid repeating yourself
        int daysInMonth = -1; // -1 indicates unknown / bad value
        switch (monthInteger)
        {
            case 2: // feb
                daysInMonth = 28; // if leap year it would be 29, but no way of indicating leap year per problem constraints
                break;
            case 3: // mar
            case 5: // may
            case 7: // jul
            case 8: // aug
            case 10: // oct
            case 12: // dec
                daysInMonth = 31;
                break;
            case 1: // jan
            case 4: // apr
            case 6: // jun
            case 9: // sep
            case 11: // nov
                daysInMonth = 30;
                break;
        }
        return daysInMonth;
    }
}
包含

GetDaysInMonth_NoLeapYear_Compact只是为了说明case掉穿行为,该行为允许多个case语句转到相同的代码。