//方法类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cp4 { class TextAnalyzer { //初始化元音、字母、数字、空格的计数器 int numVowel = 0; //元音计数 int numLetters = 0; //字母计算 int numDigits = 0; //数字计算 int numWhitesoaceChars = 0; int numWord = 0; //单词计数 char ch; public void Count(string myText) { int index = 0; while (index < myText.Length) { ch = myText[index]; if ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')) //判断是否为元音 { numVowel++; } if (char.IsLetter(ch)) //判断是否为字母 { numLetters++; } if (char.IsDigit(ch)) //判断是否为数字 { numDigits++; } if (char.IsWhiteSpace(ch)) //判断是否为空白 { numWhitesoaceChars++; } index++; } numWord = numWhitesoaceChars+1; Console.WriteLine("文本分析结果:"); Console.WriteLine("元音个数:{0:N0}",numVowel); Console.WriteLine("辅音个数:{0:N0}",numLetters-numVowel); Console.WriteLine("字母个数:{0:N0}",numLetters); Console.WriteLine("数字个数:{0:N0}",numDigits); Console.WriteLine("单词个数:{0:N0}",numWord); } } } //测试类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cp4 { class Test { static void Main(string[] args) { TextAnalyzer te = new TextAnalyzer(); Console.Write("请输入要分析的字符串:"); //接受输入并转换成大写字母 string mytext = Console.ReadLine().ToUpper(); te.Count(mytext); } } }