【发布时间】:2016-04-18 18:11:17
【问题描述】:
我有 7 个数组被分别读取,然后使用它们组合成一个数组,这是我的“记录”数组。
这是我的记录结构:
public class Record
{
// Each of the data
public string month;
public int afDays;
public int years;
public double rainfall;
public double sunshineHours;
public double maxTemp;
public double minTemp;
// Constructor
public Record(string newMonth, int newAfDays, int newYears, double newRainfall, double newSunshine, double newMaxTemp, double newMinTemp)
{
// Set the instance variables to each of the given values.
month = newMonth;
afDays = newAfDays;
years = newYears;
rainfall = newRainfall;
sunshineHours = newSunshine;
maxTemp = newMaxTemp;
minTemp = newMinTemp;
}
}
然后我希望通过使用用户输入“年份”来搜索这个数组,然后搜索这个记录数组并显示所有匹配年份的数组条目的完整记录。
这就是记录数组的构造方式
// Set the size of the record array, using the length of month - as its the size of the data set.
recordArray = new Record[months.Length];
for (int i = 0; i < months.Length; i++)
{
// Make a new record using each of the elements of each array.
recordArray[i] = new Record(months[i], afDays[i], years[i], rainfall[i], sunshineHours[i], maxTemp[i], minTemp[i]);
}
然后我希望使用类似于以下的语句来搜索数组:
case ConsoleKey.D4: //Search and display by Year
Console.WriteLine("Please input a year");
int userYear = 0;
userYear = int.Parse(Console.ReadLine());
for (int i = 0; i < analyser.years.Length; i++) //unfinished
{
if (analyser.recordArray.years = useryears)
}
【问题讨论】:
-
目前看起来还不错 - 你有什么问题?您认为
if语句中应该包含什么内容? -
提示:
=用于分配;==用于比较。接下来,考虑如何在循环中使用i。 -
好吧,我想只在当前记录与用户输入年份匹配时才打印出当前记录,因此如上所示的 analyser.recordArray.years == useryears 行。但我得到“记录不包含“年”等的定义......
-
recordArray[i].years。整个记录数组没有 years 属性。来自它的一条记录,确实如此。
-
将尝试此@ManoDestra 并报告。提前致谢!编辑:完美运行,谢谢!