我正在自学 C#,当前章节的挑战之一要求我提示用户输入字符串,写回字符串,计算字符数,字母“e”的实例,最后是所有元音的实例。它给出了使用提示,switch
但我不知道该怎么做。我确实通过手动完成它来工作,但我认为这不是重点。:) 我如何使用switch
语句来减少键入的行数?
Console.WriteLine("Please type a sentence and hit enter: ");
string myString = Console.ReadLine();
int letterCount = myString.Split('e').Length - 1;
Console.Clear();
Console.WriteLine("Thank you. The sentence you entered was: \n\"{0}\"", myString);
Console.WriteLine("This sentence is {0} characters long.", myString.Length);
Console.WriteLine("It contains {0} instances of the letter \'e\'.", letterCount);
int vowelCount = 0;
int letterALower = myString.Split('a').Length - 1;
vowelCount += letterALower;
int letterELower = myString.Split('e').Length - 1;
vowelCount += letterELower;
int letterILower = myString.Split('i').Length - 1;
vowelCount += letterILower;
int letterOLower = myString.Split('o').Length - 1;
vowelCount += letterOLower;
int letterULower = myString.Split('u').Length - 1;
vowelCount += letterULower;
int letterAUpper = myString.Split('A').Length - 1;
vowelCount += letterAUpper;
int letterEUpper = myString.Split('E').Length - 1;
vowelCount += letterEUpper;
int letterIUpper = myString.Split('I').Length - 1;
vowelCount += letterIUpper;
int letterOUpper = myString.Split('O').Length - 1;
vowelCount += letterOUpper;
int letterUUpper = myString.Split('U').Length - 1;
vowelCount += letterUUpper;
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();