【发布时间】:2017-05-18 18:36:49
【问题描述】:
我有以下代码,如何从列表框中的项目中获取值并让我的程序找到值的平均值、最高和最低值。我目前有以下文本框(averageTextbox、highestTextbox、lowestTextbox),我希望将值相应地显示到文本框中。提前致谢!
private void readButton_Click(object sender, EventArgs e)
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(
@"C:\Users\Harra\Documents\Visual Studio 2017\Projects\File Reader\Sales.txt");
double dblAdd = 0;
while ((line = file.ReadLine()) != null)
{
displayListBox.Items.Add(line);
dblAdd += Convert.ToDouble(line);
counter++;
}
totalTextBox.Text = string.Format("{0:F}", dblAdd);
}
【问题讨论】:
-
您在每次迭代时都会在
Convert.ToDouble(line)中获得双精度值。在循环中进行计算。您所做的只是计算一个总和,但您可以轻松地将转换后的 double 分配给一个局部变量,并用它做任何事情。 -
您好,我希望保留总和计算。我不太明白你在说什么?抱歉,我真的是编程新手
-
查看答案。您应该能够弄清楚它在做什么,因为它非常基础,但如果您有任何问题,请随时提出。
-
您好,感谢您的回答。如何将最高、最低和平均值显示到相应的文本框中?我的文本框被称为:highestTextbox、lowestTextbox 和 averageTextbox。再次感谢
-
您知道如何在
totalTextbox中输入值,不是吗?对其他人也这样做。
标签: c# listbox average highest