将int输入与数组的位置进行比较

时间:2015-02-25 17:43:02

标签: c# arrays

C#noob在这里,试图尝试解决基本问题的不同方法。我想将一个参数传递给一个方法,并在该方法中循环一个月的数组。如果参数等于数组的位置,我想返回该数组位置的字符串。

我尝试过以下方法:

class Month
{
    private int month;

    public string strMonth(int month)
    {
        this.month = month;

        string[] months = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

        for (int i = 0; i < months.Length; i++)
        {
            if (month == Array.IndexOf(months, i))
            {
                return months[i];
            }
        }

        return "check fails";
    }
}

对于我的司机我正在使用

class Program
{
    static void Main(string[] args)
    {
        Month testMonth = new Month();

        Console.WriteLine(testMonth.strMonth(2));

        Console.ReadKey();
    }
}

但是,我总是在控制台中登录check fails。我是走在正确的道路上还是取消了诺言,我完全错了?我也对块级别范围感到困惑(我认为C#的作用是什么?)。我来自JS背景,我已经习惯了水平范围。即使我的支票通过,添加return "check fails"总是会执行吗?

7 个答案:

答案 0 :(得分:1)

我不是C#的专家,但是为什么不回复月份[月]而不需要循环?

班级月 {     private int month;

public string strMonth(int month)
{
    this.month = month;

    string[] months = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };


    return months[month -1];
}

}

答案 1 :(得分:1)

更清洁的方法是使用字典。类似的东西:

class Month
{
    public string strMonth(int month)
    {
        var months = new Dictionary<int, string>
        {
            {1, "Jan"},
            {2, "Feb"},
            {3, "March"},
            {4, "April"},
            {5, "May"},
            {6, "June"},
            {7, "July"},
            {8, "Aug"},
            {9, "Sept"},
            {10, "Oct"},
            {11, "Nov"},
            {12, "Dec"}
        };

        var monthString = "check fails";
        if (months.ContainsKey(month))
        {
            monthString = months[month];
        }
        return monthString;
    }
}

答案 2 :(得分:0)

这应该这样做,并防止非法索引尝试访问该阵列并发送异常消息。

class Month
{
    private int month;

    public string strMonth(int month)
    {
        this.month = month;

        string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        if (month <= months.Length && month > 0)
            return months[i-1];
    }
    return "check fails";
}

答案 3 :(得分:0)

阅读Array.IndexOf的文档会清楚地表明错误:

  

搜索指定的对象并返回其第一个索引   在一维数组中出现。

https://msdn.microsoft.com/en-us/library/7eddebat(v=vs.110).aspx

所以你正在做的是在数组中搜索int 2并且它没有找到它所以返回-1当然永远不会等于你的输入。

做你想做的事的最好方法就是return months[month]

答案 4 :(得分:0)

如果您只需要一个月份名称,则可以使用DateTimeFormatInfo.GetMonthName()

执行此操作

例如 public string strMonth(int month) { return System.Globalization.CultureInfo .CurrentCulture.DateTimeFormat.GetMonthName(month); }

答案 5 :(得分:0)

将索引转换为数组元素有很多不必要的代码。你真的是在思考这个问题。

public string strMonth(int index)
{
    string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    return months[index]; //0-index array (Jan = 0)
}

如果您尝试将基于1的索引(Jan = 1)转换为字符串,只需使用return months[index - 1];从索引中减去1。然后,您可以避免将空字符串作为可能的结果。

与往常一样,请考虑您希望处理可能的ArrayIndex异常的位置 如果要验证此方法中的索引(而不是让Exception冒泡到调用方法),可以添加此检查:

public string strMonth(int index)
{
    if (index < 0|| index > 11) //or 1, 12 for the 1-indexed version
         return "Failed.";

    string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    return months[index]; //0-indexed array (Jan = 0)
}

答案 6 :(得分:0)

using System;

namespace Test
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string[] data = new string[] { "a", "b", "c", "d", "e", "f" };

            int input = Convert.ToInt32 (Console.ReadLine ());
            try {
                Console.WriteLine (data [input]);
            } catch (IndexOutOfRangeException) {
                Console.WriteLine ("Invalid input.");
            }
        }
    }
}