2016-12-19 118 views
-4
  int total = 0; 
      int wordCount = 0, index = 0; 
      var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; 
      var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' }; 

      for (int i = 0; i < sentence.Length; i++) 

        if (vowels.Contains(sentence[i])) 
        { 
         total++; 
        } 
        else if (consonants.Contains(sentence[i])) 
        { 
         total++; 
        } 

       } 
       Console.WriteLine("Your total number of vowels is: {0}", total); 
       Console.WriteLine("Number of consonants: {0}", total); 
       Console.ReadLine(); 
` 

这是我的代码。当我运行我的代码时,它准确地告诉我有多少元音,但它没有告诉我辅音的数量。它只复制了元音的数量。计数元音和辅音字符串

+1

您是否意识到这是您第二次询问并且..它具有完全相同的代码以显示相同的“总计”变量..?作为一个开始的地方,在不同的变量中计数你的辅音和元音。 –

+1

所以'总数'是元音的数量和辅音的数量?这是如何运作的? – John3136

+0

@ John3136,看起来像op没有阅读(或试图理解)他自己的代码。**当我运行我的代码时,它准确地告诉我有多少元音,但它没有告诉我辅音的数量。它只是复制了元音的数量。** –

回答

3
 int totalVowels = 0; 
     int totalConsonants = 0; 
     int wordCount = 0, index = 0; 
     var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; 
     var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' }; 

     for (int i = 0; i < sentence.Length; i++) 
     { 
       if (vowels.Contains(sentence[i])) 
       { 
        totalVowels++; 
       } 
       else if (consonants.Contains(sentence[i])) 
       { 
        totalConsonants++; 
       } 

      } 
      Console.WriteLine("Your total number of vowels is: {0}", totalVowels); 
      Console.WriteLine("Number of consonants: {0}", totalConsonants); 
      Console.ReadLine(); 
1

在这里你需要考虑几件事情来实现你的目标。输入字符串可能包含或不可以包含其他字符(特殊字符或数字),因此您应该检查输入字符串中的每个字符是否存在于vowelsconsonants中,还有一件事,您必须为vowelsconsonants保留单独的计数器,例如命名为vowelsCount,consonantsCount。这意味着如果字符出现在vowels的集合中,那么应该增加vowelsCount,并且如果它存在于consonants中,那么应该增加consonantsCount

此外,如果需要,您可以保留另一个变量来计算非字母字符数。现在看看下面的代码:

int vowelsCount = 0, consonantsCount = 0, otherCharacterCount = 0; 
string inputSenctnse = Console.ReadLine().ToLower(); 
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; 
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' }; 
foreach (char letter in inputSenctnse) 
{ 
    if (vowels.Contains(letter)) 
     vowelsCount++; 
    else if (consonants.Contains(letter)) 
     consonantsCount++; 
    else 
     otherCharacterCount++; 

} 
Console.WriteLine("Your total number of vowels is: {0}", vowelsCount); 
Console.WriteLine("Number of consonants: {0}", consonantsCount); 
Console.WriteLine("Other characters : {0}", otherCharacterCount); 
Console.ReadLine(); 
0

您需要使用两个变量来保持两个数字为Vinh Vu。

我只是张贴在这里另一种解决方案,但仍需要变量,以保持两者:

int total = 0; 
int wordCount = 0, index = 0; 
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; 
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' }; 
var sentence = "my sentence"; 
var vowelCount = sentence.ToCharArray().Where(x => vowels.Contains(x)).Count(); 
var consonantCount = sentence.ToCharArray().Where(x => consonants.Contains(x)).Count(); 
0

启动另一个变量名。在这里我添加了变量total2。

int total = 0; 
int total2 = 0; 
     int wordCount = 0, index = 0; 
     var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' }; 
     var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' }; 

     for (int i = 0; i < sentence.Length; i++) 

       if (vowels.Contains(sentence[i])) 
       { 
        total++; 
       } 
       else if (consonants.Contains(sentence[i])) 
       { 
        total2++; 
       } 

      } 
      Console.WriteLine("Your total number of vowels is: {0}", total); 
      Console.WriteLine("Number of consonants: {0}", total2); 
      Console.ReadLine(); 
相关问题