需要搜索6个txt文件,然后将它们一起排序。任何开始的想法?
答案 0 :(得分:1)
如果我假设您的数据已按升序排序,您的用户输入将为年和月。例如,他们希望查看2014年7月的天气数据,然后您可以使用数组的索引作为相同的行号指示符。请查看以下代码:
static void Main(string[] args)
{
string yearInput = "2014"; // User input
string monthInput = "July"; // User input
string[] Year = { "2013", "2014", "2014", "2014", "2014", "2014", "2015" };
string[] Month = { "July", "July", "August", "September", "October", "November", "December", "January" };
string[] rain1 = { "0", "1", "2", "3", "4", "5", "6", "7" };
string[] sun1 = { "0a", "1a", "2a", "3a", "4a", "5a", "6a", "7a" };
var yearIndex = Array.FindIndex(Year, year => year == yearInput);
var monthIndex = Array.FindIndex(Month, yearIndex, month => month == monthInput);
var outputRain = rain1[monthIndex]; // The corresponding rain for the input
var outputSun = sun1[monthIndex]; // The corresponding sun for the input
}
Array.FindIndex
从MSDN开始,到searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it.
从那里,我们可以找到指定年份的第一次出现的索引,并将其用作startIndex
来搜索指定月份的索引,以便跳过2013年并从2014年开始操作。
根据我的假设得到monthIndex
之后,我们可以将其视为行号并获取天气数据。希望它有所帮助。