gpt4 book ai didi

c# - 如何在 C# 中添加外部文件的数据量(流读取器)

转载 作者:行者123 更新时间:2023-12-02 21:43:29 24 4
gpt4 key购买 nike

我是这个网站的新手,对编程也很陌生,目前我正在尝试显示我的捐赠列表上用户姓名的计数金额,然后我还想计算出一个总和捐赠列表包含的总金额,如果有人可以帮助我创建一种方法来在donations.txt文件上添加捐赠者的数量,那将有很大的帮助,我不知道从哪里开始,但到目前为止这是我的编码:

        string sName;
double dAmount;
string sTotalNames;
double dAmountTotal;
double dAmountAverage;

using (StreamReader sr = new StreamReader("Donations.txt"))
{
while (sr.Peek() != -1)
{
sName = sr.ReadLine();
Console.WriteLine(sName);

dAmount = Convert.ToDouble(sr.ReadLine());
Console.WriteLine(dAmount);
}

Console.WriteLine("Press any key to close");
Console.ReadKey();
}

最佳答案

假设您的其他一切工作正常,创建总和将非常容易。

string sName;
double dAmount;
int sTotalNames = 0;
double dAmountTotal = 0;
double dAmountAverage;

using (StreamReader sr = new StreamReader("Donations.txt"))
{
while (sr.Peek() != -1)
{
sName = sr.ReadLine();
Console.WriteLine(sName);

dAmount = Convert.ToDouble(sr.ReadLine());
Console.WriteLine(dAmount);
dAmountTotal += dAmount;
sTotalNames++;
}
dAmountAverage = dAmountTotal / sTotalNames;
Console.WriteLine("Sum = {0}", dAmountTotal );
Console.WriteLine("Total Names = {0}", sTotalNames);
Console.WriteLine("Average Amount = {0}", dAmountAverage);
Console.WriteLine("Press any key to close");
Console.ReadKey();
}

关于c# - 如何在 C# 中添加外部文件的数据量(流读取器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com