我有一个文本文件,其内容与下面类似,我想添加每一列我已经将它们分别放在一个数组中并拆分并命名只需要计算双打
示例文件内容:
1,cow,2007,134.50,74.90,14.4
2,goat,2012,112.32,66.50,3.2
代码:
Livestock[] animals = new Livestock[20];
int counter = 0;
string myLine;
string[] words;
TextReader tr = new StreamReader("S:/BIT694/livestock.txt");
while ((myLine = tr.ReadLine()) != null)
{
words = myLine.Split(',');
int ID = int.Parse(words[0]);
string LivestockType = words[1];
int YearBorn = int.Parse(words[2]);
double CostPerMonth = double.Parse(words[3]);
double CostOfVaccination = double.Parse(words[4]);
double AmountMilk = double.Parse(words[5]);
eConsole.WriteLine("Calculation of farm profit: ");
Console.Write("Enter price of milk: $");
double PriceOfMilk = double.Parse(Console.ReadLine());
Livestock SumOfMilk = null;
PriceOfMilk = PriceOfMilk * SumOfMilk.amountMilk * 365;
double cost = SumOfMilk.costPerMonth + SumOfMilk.costOfVaccination;
double total = PriceOfMilk - cost;
Console.WriteLine("Farm profit: ${0}", total);
Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Press any key to return to the main menu...");
答案 0 :(得分:0)
执行此操作的一种方法是在Livestock
循环中创建新的foreach
对象,并将其添加到animals
列表中。然后你就可以获得每个项目的Sum
。
首先,这是System.Linq
从您的文件中创建Livestock
列表的方法:
List<Livestock> animals = File.ReadLines("S:/BIT694/livestock.txt")
.Select(line => line.Split(','))
.Select(words => new Livestock
{
ID = int.Parse(words[0]),
LivestockType = words[1],
YearBorn = int.Parse(words[2]),
CostPerMonth = double.Parse(words[3]),
CostOfVaccination = double.Parse(words[4]),
AmountMilk = double.Parse(words[5])
})
.ToList();
然后你可以输出总数,如:
Console.WriteLine($"Total cost per month: {animals.Sum(a => a.CostPerMonth)}");
Console.WriteLine($"Total cost of vaccinations: {animals.Sum(a => a.CostOfVaccination)}");
Console.WriteLine($"Total amount of milk: {animals.Sum(a => a.AmountMilk)}");
如果第一个代码段中的Linq
令人困惑,则与以下内容相同:
List<Livestock> animals = new List<Livestock>();
foreach (var line in File.ReadLines("S:/BIT694/livestock.txt"))
{
var words = line.Split(',');
var animal = new Livestock
{
ID = int.Parse(words[0]),
LivestockType = words[1],
YearBorn = int.Parse(words[2]),
CostPerMonth = double.Parse(words[3]),
CostOfVaccination = double.Parse(words[4]),
AmountMilk = double.Parse(words[5])
};
animals.Add(animal);
}
答案 1 :(得分:0)
我已经想出了如何做到这一点我希望这有助于其他人
Console.WriteLine("Calculation of farm profit: ");
Console.Write("Enter price of milk: $");
double PriceOfMilk = double.Parse(Console.ReadLine());
double sumOfMilk = 0.0;
// loop through all livestocks (the array)
for (int i = 0; i < 20; i++)
{
sumOfMilk += animals[i].amountMilk;
}// end of for loop sumOfMilk
double yearIncome = sumOfMilk * 365 * PriceOfMilk; // total income
double cost = 0.0;
for (int i = 0; i < 20; i++)
{
cost += animals[i].costOfVaccination; // vaccination
cost += animals[i].costPerMonth * 12;
}// end of for loop Vaccination
double total = yearIncome - cost;
// Farms total profit for the year
Console.WriteLine("Farm profit: ${0}", total.ToString("0.00"));
Console.WriteLine();
Console.WriteLine("Press any key to return to the main menu...");
Console.ReadLine();
Console.Clear();