我必须完成一个程序,其中包括打开一个包含数字的文件,显示它,然后还显示该文件有多少个数字以及它们的总和。
我目前对如何将所有读取的数字相加并显示:/
这是我的代码:
private void btnReadRandomNumbers_Click(object sender, EventArgs e)
{
StreamReader inputFile;
try
{
int number = 0;
int count = 0;
int sum = 0;
lstRandomNumbers.Items.Clear();
if (fodOpenFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(fodOpenFile.FileName);
lstRandomNumbers.Items.Clear();
while (!inputFile.EndOfStream)
{
number = int.Parse(inputFile.ReadLine());
count = count + 1;
lstRandomNumbers.Items.Add(number);
}
lblNumberCount.Text = count.ToString();
lblSumNumbers.Text = number.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show("There is a problem with the disk file." + Environment.NewLine + ex.Message, "User Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
As seen in the Picture, the sum is only reading the last number of the list and im not sure why
感谢阅读!
答案 0 :(得分:1)
第number = int.Parse(inputFile.ReadLine());
行中的每行替换了数字!
您可以使用以下代码编写所有代码:
private void btnReadRandomNumbers_Click(object sender, EventArgs e)
{
try
{
lstRandomNumbers.Items.Clear();
if (fodOpenFile.ShowDialog() == DialogResult.OK)
{
var linesOfFile = File.ReadAllLines(fodOpenFile.FileName).Select(int.Parse).ToList();
lblSumNumbers.Text = linesOfFile.Sum().ToString();
lblNumberCount.Text = linesOfFile.Count().ToString();
lstRandomNumbers.DataSource = linesOfFile;
}
}
catch (Exception ex)
{
MessageBox.Show("There is a problem with the disk file." + Environment.NewLine + ex.Message, "User Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
答案 1 :(得分:0)
您快到了。稍作修改即可尝试使用相同的代码。我添加了嵌入式注释。
private void btnReadRandomNumbers_Click(object sender, EventArgs e)
{
StreamReader inputFile;
try
{
int number = 0;
int count = 0;
int sum = 0;
//lstRandomNumbers.Items.Clear(); //don't need this
if (fodOpenFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(fodOpenFile.FileName);
//lstRandomNumbers.Items.Clear();//don't need this
while (!inputFile.EndOfStream)
{
number = int.Parse(inputFile.ReadLine());
count = count + 1;
//lstRandomNumbers.Items.Add(number);//don't need this
sum+=number; // add this
}
lblNumberCount.Text = count.ToString();
lblSumNumbers.Text = sum.ToString(); //change `number' to `sum`
}
}
catch (Exception ex)
{
MessageBox.Show("There is a problem with the disk file." + Environment.NewLine + ex.Message, "User Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
@Issa,如果有帮助,请告诉我。
答案 2 :(得分:0)
尝试一下:
private void btnReadRandomNumbers_Click(object sender, EventArgs e)
{
if (fodOpenFile.ShowDialog() == DialogResult.OK)
{
var lines = File.ReadAllLines(fodOpenFile.FileName);
var result =
lines
.Select(x => int.Parse(x))
.Aggregate(
new { count = 0, sum = 0 },
(a, x) => new { count = a.count + 1, sum = a.sum + x });
lstRandomNumbers.Items.Clear();
lstRandomNumbers.Items.AddRange(lines);
lblNumberCount.Text = result.count.ToString();
lblSumNumbers.Text = result.sum.ToString();
}
}
答案 3 :(得分:-1)
static void Main(string[] args)
{
var res = 0;
var r = new Regex(@"\d");// you can try "\d+" and you will see the difference
var matches = r.Matches("feawfwe312faewfa4gaeg1feaga67awfaw2");
if (matches != null && matches.Count > 0)
{
foreach (var m in matches)
res += int.Parse(m.ToString());
}
Console.WriteLine(res);
Console.ReadKey();
}