在调用方法时使用for循环

本文关键字:for 循环 调用 方法 | 更新日期: 2023-09-27 18:24:01

我在一个方法中使用for循环将结果传递到主函数。我正试图使用for循环来获取一年中的月份,并将其传递给主函数的输出。

我在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();
    }
}

在调用方法时使用for循环

我认为从DateTimeFormat使用GetMonthName是更好的方法。这将在用户活跃的区域性中为您命名。(当然,您可以将其硬编码为任何您喜欢的区域性)然后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);
}

您可以使用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";
        }

做这样的事情来代替

string[] months = new string[12] {"January", "February", "March" }; // Input all months to this array
if (index <= -1 || index > 12) return "N/A";
return months[index];

将此代码插入getMonthName()函数

不要使用循环和if-else。你需要的是字典。

static Dictionary<int, string> _monthName = new Dictionary<int, string>
{
    {1,"January" }, // if zero based start from 0
    {2,"February" },
    {3,"March" },
    {4,"April" },
    {5,"May" },
    {6,"June" },
    {7,"July" },
    {8,"August" },
    {9,"September" },
    {10,"October" },
    {11,"November" },
    {12,"December" },
};
private static string GetMonthName(int i)
{
    var result = "";
    if (_monthName.TryGetValue(i, out result))
    {
        return result;
    }
    return "N/A";
}
static void Main(string[] args)
{
    Console.WriteLine("Month 1: " + GetMonthName(1));
    Console.WriteLine("Month 2: " + GetMonthName(2));
    Console.WriteLine("Month 3: " + GetMonthName(3));
    Console.WriteLine("Month 4: " + GetMonthName(4));
    Console.WriteLine("Month 5: " + GetMonthName(5));
    Console.WriteLine("Month 6: " + GetMonthName(6));
    Console.WriteLine("Month 7: " + GetMonthName(7));
    Console.WriteLine("Month 8: " + GetMonthName(8));
    Console.WriteLine("Month 9: " + GetMonthName(9));
    Console.WriteLine("Month 10: " + GetMonthName(10));
    Console.WriteLine("Month 11: " + GetMonthName(11));
    Console.WriteLine("Month 12: " + GetMonthName(12));
    Console.WriteLine("Month 43: " + GetMonthName(43));
    Console.ReadKey();
}

在我看来,在这种情况下,你应该放弃在for循环中使用这么多if语句,因为它不够可读,更好的方法是创建一个具有月份所有名称的字符串类型的数组,并迭代这个数组。你的代码应该是:

public static String month_name(int month) {
        String result;
        result = "a";
        // for the sake of readability I have split the line
        String[] allMonths = { 
                              "N/A", "January", "February", "March", "April",
                              "May", "June", "July", "August", "September", 
                              "October", "November", "December" 
                          };
        if (month >= 0 && month <= 12)
            result = allMonths[month];
        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();
    }

希望它能有所帮助:)

使用Swith Case语句是此处的最佳选择。

但是,如果您不知道如何使用它,您应该删除For语句,因为它完全没有意义。

 //for (int i = 0; i < 12; ++i )
    //{
    //}