我正在嘗試從文本文件中計算單詞的數量,即此數字以開始計算。計算文本文件中單詞的數量
這是對字數統計程序的測試。這只是一個測試。如果您的 程序成功運行,則應計算此文件中有30個 單詞。
我使用的StreamReader把一切都從文件轉換成字符串,然後使用.Split方法得到的個別單詞的數量,但我不斷收到錯誤的值當我編譯和運行程序。
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,.";
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
}
fields = line.Split(delim.ToCharArray());
for(int i = 0; i < fields.Length; i++)
{
counter++;
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
不同於論壇的網站,我們不使用「謝謝」,或者「任何幫助表示讚賞」,或簽名(因此)。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be請參閱「[應該在標題中包含」標籤?「(http://meta.stackexchange.com/questions/19190/)」), –