C# 比较int输入和数组位置
C#noob在这里,试图尝试解决一个基本问题的不同方法。我想把一个参数传递给一个方法,在这个方法中我循环遍历一个月数组。如果参数等于数组的位置,我想返回该数组位置的字符串 我尝试了以下方法:C# 比较int输入和数组位置,c#,arrays,C#,Arrays,C#noob在这里,试图尝试解决一个基本问题的不同方法。我想把一个参数传递给一个方法,在这个方法中我循环遍历一个月数组。如果参数等于数组的位置,我想返回该数组位置的字符串 我尝试了以下方法: class Month { private int month; public string strMonth(int month) { this.month = month; string[] months = { " ", "January",
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";
}
}
但是,我总是在控制台中记录
检查失败
。我走的是正确的道路吗?还是无所谓的东西占了上风,而我完全错了?我还对块级别的范围划分感到困惑(我认为C#就是这么做的?)。我来自JS背景,我习惯于功能级别的范围。添加返回“check fails”
时,即使我的检查通过了,也会一直执行吗?我在这里并不是一个C#方面的专家,但为什么不直接返回months[month],而不需要for循环呢
班级月份
{
私人整数月
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];
}
}这样做可以防止非法索引试图访问数组并发送异常消息
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";
}
班级月
{
私人整数月;
公共字符串strMonth(整数月)
{
本月=月;
字符串[]个月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
如果(第0个月)
返回月份[i-1];
}
返回“检查失败”;
}
阅读数组的文档。IndexOf
将明确错误:
搜索指定的对象并返回其第一个对象的索引
出现在一维数组中
所以你要做的是在数组中搜索int 2,它没有找到它,所以返回-1,当然它永远不会等于你的输入
做你想做的事情的最好方法就是
返回月份[month]
如果你只需要一个月名,你可以用它来做
例如
公共字符串strMonth(整数月)
{
return System.Globalization.CultureInfo
.CurrentCulture.DateTimeFormat.GetMonthName(月);
}
要将索引转换为数组元素,有很多不必要的代码。你真的想得太多了
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)转换为字符串,只需使用returnmonths[index-1]从索引中减去1即可代码>。然后,避免将空白字符串作为可能的结果。
和始终一样,考虑您想处理可能的ARARYEQUEST异常的位置。
如果要验证此方法内的索引(而不是让异常冒泡到调用方法),可以添加以下检查:
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)
}
公共字符串strMonth(int索引)
{
if(index<0 | | index>11)//或1,12表示单索引版本
返回“失败”;
字符串[]个月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
返回月份[索引];//0-索引数组(Jan=0)
}
更干净的方法是使用字典。比如:
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;
}
}
班级月
{
公共字符串strMonth(整数月)
{
var月=新字典
{
{1,“Jan”},
{2,“二月”},
{3,“三月”},
{4,“四月”},
{5,“五月”},
{6,“六月”},
{7,“7月”},
{8,“八月”},
{9,“9月”},
{10,“十月”},
{11,“11月”},
{12,“Dec”}
};
var monthString=“检查失败”;
如果(月数)
{
monthString=月[月];
}
返回月弦;
}
}
您知道“一月”必须插入0,对吗?是的,我知道。我有一个空字符串[0]来欺骗支票。谢谢你指出这一点。这与我的问题无关。如果month
超出范围,这将抛出。我现在不在电脑上,所以我不能检查任何东西,但month-1不应该做这个把戏吗?不客气。它还降低了复杂性,因为您不必遍历数组。如果问题真的是从一个数字中获得一个月的名称,那么可能会引起兴趣,并且具有为不同文化工作的优势,等等。@Chris它确实不是,我只是想尝试将int参数与数组中的某个位置进行匹配,这似乎是一个很好的用例。@Dann:我确实怀疑,但当你接受了一个答案,而不是通过位置从数组中取出东西时,我变得不确定了。ie接受的答案似乎保存了问题中的“按整数获取月份”部分,而不是“从现有数组获取值”部分,甚至没有说明代码不起作用的原因。只要你的身体好,那么一切都好。:)@Chris我看到了一些其他的答案,这些答案似乎解决了为什么我的代码不起作用的问题,而且更符合我最初的问题,但我最喜欢这个解决方案。我以前从未使用过词典,在我看来,这似乎是一个很好的选择:)
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;
}
}
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.");
}
}
}
}