using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace FunkcjaSpilit
{
class Program2
{
static int _MinWordLength = 7;
static void Main()
{
DirectoryInfo filePaths = new DirectoryInfo(@"D:\project_IAD");
FileInfo[] Files = filePaths.GetFiles("*.sgm");
List<int> firstone = new List<int>();
foreach (FileInfo file in Files)
{
int longWordsCount = CalculateLongWordsCount(file, _MinWordLength);
string justFileName = file.Name;
firstone.Add(longWordsCount);
Console.WriteLine(("W pliku: " + justFileName) + " liczba długich słów to " + longWordsCount);
}
Console.WriteLine(firstone.Count);
Console.ReadLine();
}
private static int CalculateLongWordsCount(FileInfo file, int _MinWordLength)
{
return File.ReadLines(file.FullName).
Select(line => line.Split(' ').Count(word => word.Length > _MinWordLength)).Sum();
}
}
}
我在这一行的代码firstone.Add(longWordsCount);我想将长度超过 7 的所有单词添加到列表中,但是在运行此代码后,firstone.Add(longWordsCount);只添加目录中的文件总和(而不是长度超过 7 的所有单词的总和。 sgm 目录中的文件)。
我该如何解决?
预期成绩:
firstone.Add(longWordsCount);
应该将所有长度超过 7 的单词从目录中的所有文件中添加到列表中。