本文介绍了我想在任何文字中找出元音。但不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { int vowel; vowel=0; int i=0; Console.WriteLine("Enter Your Text:\n"); char[] stringName = new char[10]; for (int u = 0; u < stringName.length; u++) { stringName[u] =Console.ReadLine(); } Console.WriteLine("Your text size is {0} character long\n",stringName.Length); while (i <stringName.Length) { if (stringName[i] == 'a' || stringName[i] == 'e' || stringName[i] == 'i' || stringName[i] == 'o' || stringName[i] == 'u' || stringName[i] == 'A' || stringName[i] == 'E' || stringName[i] == 'I' || stringName[i] == 'O' || stringName[i] == 'U') vowel++; i++; } Console.WriteLine(vowel); Console.ReadLine(); } }} 推荐答案 static void Main(){ // Build a list of vowels up front: var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; Console.WriteLine("Enter a Sentence"); string sentence = Console.ReadLine().ToLower(); int total = sentence.Count(c => vowels.Contains(c)); Console.WriteLine("Your total number of vowels is: {0}", total); Console.ReadLine();} Console.WriteLine("Enter Your Text:");string stringName = Console.ReadLine();Console.WriteLine("Your text size is {0} character long", stringName.Length); 您也不需要在格式字符串的末尾加上 \ n ,因为 Console.WriteLine 会自动添加它。Also you do not need to put the \n at the end of your format string, since Console.WriteLine adds it automatically. //using System;using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace console_poc{ class Program { static void Main(string[] args) { int vowel = 0, number = 0,specialcharacters =0; Console.WriteLine("Enter Your Text:\n"); string name = Console.ReadLine(); Console.WriteLine("Your text size is {0} character long\n", name.Length); char[] vowels = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' }; char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; char[] specialCharacters = { '!','@',' ',' 这篇关于我想在任何文字中找出元音。但不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 23:32