【发布时间】:2018-10-19 06:41:21
【问题描述】:
我希望你们都度过了愉快的一天。所以我修复了我的程序的一个错误,但还有另一个错误:/
所以这是我创建并从文件中读取数据的代码:
void ReadData(string fileName, Branch[] branches)
{
string shopsName = null;
using (StreamReader reader = new StreamReader(@fileName))
{
string line = null;
line = reader.ReadLine();
if (line != null)
{
shopsName = line;
}
Branch tempBranches = TempBranch(branches, shopsName);
string address = reader.ReadLine();
string phoneNumber = reader.ReadLine();
while (null != (line = reader.ReadLine()))
{
string[] values = line.Split(';');
string facturer = values[0];
string model = values[1];
double capacity = double.Parse(values[2]);
string energyClass = values[3];
string assemblyType = values[4];
string color = values[5];
string attribute = values[6];
double cost = double.Parse(values[7]);
Fridges fridge = new Fridges(facturer, model, capacity, energyClass, assemblyType, color, attribute, cost);
tempBranches.fridges.AddFridge(fridge);
}
}
还有我使用 TempBranch 方法的代码。错误在这一行:if (branches[i].ShopsName == shopsName)。希望您能帮助我,因为我昨天尝试修复此问题 30 分钟,但仍然无法正常工作:D
private static Branch TempBranch(Branch[] branches, string shopsName)
{
for (int i = 0; i < MaxNumberOfFridges; i++)
{
if (branches[i].ShopsName == shopsName)
{
return branches[i];
}
}
return null;
}
【问题讨论】:
-
在 C# 中,如果变量实现了接口
IEnumerable,您可以执行foreach循环。试试这个:foreach(Branch b in branches) {if (b.ShopsName == shopsName) return b;}然后在循环之外你可以返回 null 就像你做的那样没有 -
问题在这里
int i = 0; i < MaxNumberOfFridges; i++,所以改为这个int i = 0; i < branches.Length; i++
标签: c#