调用方法时使用for循环

Dam*_*amo 0 c#

我在方法中使用for循环将结果传递给main函数.我正在尝试使用for循环来获取一年中的月份并将其传递给main函数的输出.

我在for循环中嵌套了一个if循环,我觉得这可能是多余的,因为for循环无论如何都会计算到最后.这可能是代码中一个基本的问题,但是我已经盯着它看了很长时间,以至于我觉得它已经烧坏了.

所有月份的输出都返回"不存在",而不是选择相关的月份.我如何从for循环中选择相关的月份,或者到目前为止我的编码方式是否可行?

namespace Month_Function_Call
    {
class Program
{
    public static String month_name(int month)
    {
        String result;
        result = "a";
        for (int i = 0; i < 12; ++i )
        {
            if (i == 0)
            {
                result = "January";
            }
            if (i == 1)
            {
                result = "February";
            }
            if (i == 2)
            {
                result = "March";
            }
            if (i == 3)
            {
                result = "April";
            }
            if (i == 4)
            {
                result = "May";
            }
            if (i == 5)
            {
                result = "June";
            }
            if (i == 6)
            {
                result = "July";
            }
            if (i == 7)
            {
                result = "August";
            }
            if (i == 8)
            {
                result = "September";
            }
            if (i == 9)
            {
                result = "October";
            }
            if (i == 10)
            {
                result = "November";
            }
            if (i == 11)
            {
                result = "December";
            }
            else
            {
                result = "N/A";
            }


        }
            return result;
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Month 1: " + month_name(1));
        Console.WriteLine("Month 2: " + month_name(2));
        Console.WriteLine("Month 3: " + month_name(3));
        Console.WriteLine("Month 4: " + month_name(4));
        Console.WriteLine("Month 5: " + month_name(5));
        Console.WriteLine("Month 6: " + month_name(6));
        Console.WriteLine("Month 7: " + month_name(7));
        Console.WriteLine("Month 8: " + month_name(8));
        Console.WriteLine("Month 9: " + month_name(9));
        Console.WriteLine("Month 10: " + month_name(10));
        Console.WriteLine("Month 11: " + month_name(11));
        Console.WriteLine("Month 12: " + month_name(12));
        Console.WriteLine("Month 43: " + month_name(43));
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

Mag*_*nus 7

我认为使用GetMonthNamefrom DateTimeFormat会是一种更好的方法.这将为您提供用户活动文化中的名称.(您当然可以将此代码硬编码到您喜欢的任何文化中)然后ToTitleCase将第一个字符作为大写字母.

public static String month_name(int month)
{
   if(month < 1 || month > 12) 
      return "N/A";
   var culture = CultureInfo.CurrentCulture;
   var name = culture.DateTimeFormat.GetMonthName(month);
   return culture.TextInfo.ToTitleCase(name);
}
Run Code Online (Sandbox Code Playgroud)

  • 首先,OP使用基于零的索引作为月份名称,这使用基于1的索引.其次,它返回值为13的空字符串,并为小于1且大于13的值抛出异常. (2认同)

小智 5

你可以使用它来制作这种清洁剂 switch

 switch (month)
        {
            case 0: return "January";
            case 1: return "February";
            case 2: return "March";
            case 3: return "April";
            case 4: return "May";
            case 5: return "June";
            case 6: return "July";
            case 7: return "August";
            case 8: return "September";
            case 9: return "October";
            case 10: return "November";
            case 11: return "December";
            default: return "N/A";
        }
Run Code Online (Sandbox Code Playgroud)