使用switch语句减少代码行

时间:2012-06-18 00:14:14

标签: c#

我正在教自己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();

4 个答案:

答案 0 :(得分:4)

我知道这不是一个很好的答案,但我无法抗拒一个班轮!

inputString.ToLower().Count(s=>"aeiou".Contains(s)); //count the vowels

答案 1 :(得分:2)

这是一个简单的解决方案:

string str = Console.ReadLine();
string low_str = str.ToLower();
Console.Clear();
Console.WriteLine("Thank you.  The sentence you entered was: \n\"{0}\"", str);
Console.WriteLine("This sentence is {0} characters long.", str.Length);

int vowelCount = 0;
int eCount = 0;

for (int i = 0; i < low_str.Length; i++)
{
    switch(low_str[i])
    {
        case 'e': eCount ++; vowelCount++; break;
        case 'a': vowelCount++; break;
        case 'o': vowelCount++; break;
        case 'i': vowelCount++; break;
        case 'u': vowelCount++; break;
        case 'y': vowelCount++; break;
    }
}

Console.WriteLine("It contains {0} instances of the letter \'e\'.", eCount);
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();

请注意,使用此方法可以在更少的行中完成(不是最好的方法,但是我们不要过于深入到框架细节:)):

int eCount = low_str.split(new char[]{'e'}) - 1;
int vowelCount = low_str.split(new char[]{'a','e','o','i','u','y'}) - 1;

答案 2 :(得分:1)

像这样(伪代码,而不是实际的C#)?

foreach (c in mySentence)
{
  c = LowerCase(c);
  switch (c) {
    case 'a' :
    case 'e' :
    case 'i' :
    case 'o' :
    case 'u' :
      nVowels ++;
      break;
    case ' ' :
    case '\t' :
      nBlanks++;
      break;
    default :
      nChars++
      break;
  }

这里有更多信息:

答案 3 :(得分:1)

我个人会用foreach和数组或元音来做。这种方式很容易扩展,如下所示:

Char[] vowels = {'e', 'a', 'o', 'i', 'u', 'y'};
string str = Console.ReadLine();
string low_str = str.ToLower();
Console.Clear();
Console.WriteLine("Thank you.  The sentence you entered was: \n\"{0}\"", str);
Console.WriteLine("This sentence is {0} characters long.", str.Length);

int vowelCount = 0;
int eCount = 0;

foreach (char chara in low_str)
{
    foreach (char vowel in vowels)
    if (vowel == chara)
        vowelCount++;
    if (chara == 'e')
        eCount++;
}

Console.WriteLine("It contains {0} instances of the letter \'e\'.", eCount);
Console.WriteLine("There are {0} vowels used.", vowelCount);
Console.ReadLine();