【问题标题】:Count Vowel and Consonant C#计数元音和辅音 C#
【发布时间】:2015-03-14 23:14:25
【问题描述】:

问题

从用户那里输入 1 个字符。它可以是元音或辅音。 用户输入后,它会询问您是否要再次输入字符,是按 Y,否按 N。

当用户说不时,你必须显示输入了多少元音和多少辅音。

请使用 For 循环来做这个问题。我认为必须使用数组。我做了这段代码,它计算元音、辅音和间距。但我不能多次输入。

我的代码没有运行多次。我只能在一行中写一个句子或一个字符,所以它只会计算那个。但我想要求用户再次输入一个字符。

我希望我的代码运行多次,以便用户可以在我解释我的问题时多次输入。

    using System;
public class vowelConsonant
{
    public static void Main()
    {
        int vowels = 0;
        int consonant = 0;
        int space = 0;

        Console.WriteLine("Enter a Sentence or a Character");
        string v = Console.ReadLine().ToLower();

        for (int i = 0; i < v.Length; i++)
            {
                if (v[i] == 'a' || v[i] == 'e' || v[i] == 'i' || v[i] == 'o' || v[i] == 'u')
                {
                    vowels++;
                }

                else if (char.IsWhiteSpace(v[i]))

                {
                    space++;
                }

                else
                {
                    consonant++;
                }
        }

        Console.WriteLine("Your total number of vowels is: {0}", vowels);
        Console.WriteLine("Your total number of constant is: {0}", consonant);
        Console.WriteLine("Your total number of space is: {0}", space);
        Console.ReadLine();
    }
}

Thanks

【问题讨论】:

  • 那里没有问题。它有效吗?它有什么作用?
  • But I cant take the input multiple times. 您需要为此使用循环。这是一个非常基本的问题。或许stackoverflow.com/questions/21118495/…可以帮到你。
  • 如果输入数字会怎样?或特殊字符(例如"#")。

标签: c#


【解决方案1】:

只需在整个过程中设置一个无限循环。

using System;
public class vowelConsonant
{
    public static void Main()
    {
        // Infinite loop.
        while (true)
        {
            int vowels = 0;
            int consonant = 0;
            int space = 0;

            Console.WriteLine("Enter a Sentence or a Character");
            string v = Console.ReadLine().ToLower();

            for (int i = 0; i < v.Length; i++)
            {
                if (v[i] == 'a' || v[i] == 'e' || v[i] == 'i' || v[i] == 'o' || v[i] == 'u')
                {
                    vowels++;
                }

                else if (char.IsWhiteSpace(v[i]))

                {
                    space++;
                }

                else
                {
                    consonant++;
                }
            }

            Console.WriteLine("Your total number of vowels is: {0}", vowels);
            Console.WriteLine("Your total number of constant is: {0}", consonant);
            Console.WriteLine("Your total number of space is: {0}", space);
        }
    }
}

【讨论】:

    【解决方案2】:

    这个应用程序计算一个句子中的元音和辅音字母。 这是另一种代码行更少的解决方案,理解使用循环和嵌套循环与 char 数组的想法。

    Application interface with controls names

    namespace Program8_4
    {
        public partial class Form1 : Form
        {
            // declare the counter variables in field
            int iNumberOfVowels = 0;
            int iNumberOfConsonants = 0;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // call the methods in this event
                GetVowels(txtStringInput.Text);
                GetConsonants(txtStringInput.Text);
                // show the result in a label
                lblOutput.Text = "The number of vowels : " + iNumberOfVowels.ToString() + Environment.NewLine+
                    "The number of consonants : " + iNumberOfConsonants.ToString();
                // assign zero the counters to not add the previous number to new number, and start counting from zero again
                iNumberOfVowels = 0;
                iNumberOfConsonants = 0;
            }
    
            private int GetConsonants(string strFindConsonants)
            {
                // Declare char array to contain consonants letters
                char[] chrConsonants = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X',
                    'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
    
                // loop to get each letter from sentence
                foreach (char Consonants in strFindConsonants)
                {
                    // another nested loop to compare each letter with all letters contains in chrConsonants array
                    for (int index = 0; index < chrConsonants.Length; index++)
                    {
                        // compare each letter with each element in charConsonants array
                        if (Consonants == chrConsonants[index])
                        {
                            // If it is true add one to the counter iNumberOfConsonants
                            iNumberOfConsonants++;
                        }
                    }
                }
                // return the value of iNumberOfConsonants
                return iNumberOfConsonants;
            }
    
            private int GetVowels(string strFindVowels)
            {
                // Declare char array to contain vowels letters
                char[] chrVowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O','U' };
    
                // loop to get each letter from sentence
                foreach (char Vowels in strFindVowels)
                {
                    // another nested loop to compare each letter with all letters contains in chrVowels array
                    for (int index = 0; index < chrVowels.Length; index++)
                    {
                        // compare each letter with each element in chrVowels array
                        if (Vowels == chrVowels[index])
                        {
                            // If it is true add one to the counter iNumberOfVowels
                            iNumberOfVowels = iNumberOfVowels+1;
                        }
                    }
                }
                // return the value of iNumberOfVowels
                return iNumberOfVowels;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我们可以使用 Regex.Matches() 函数简单地找到所需的元音和辅音计数。

      下面是工作代码sn-p:

       public static void Main()
         {
             Console.WriteLine("Enter a Sentence or a Character");
             string input = Console.ReadLine().ToLower();
      
             string vowels = @"[aeiouAEIOU]+"; //regular expression to match vowels
             string consonants = @"[^aeiouAEIOU]+"; //regular expression to match consonants
             string space = @"[\S]+";
             int vowLength = new Regex(vowels).Matches(input).Count;
             int conLength = new Regex(consonants).Matches(input).Count;;
             int spLength = new Regex(space).Matches(input).Count;;
      
             Console.WriteLine("Your total number of vowels is: {0}", vowLength);
             Console.WriteLine("Your total number of constant is: {0}", conLength);
             Console.WriteLine("Your total number of space is: {0}", spLength);
         }
      

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-14
        • 1970-01-01
        相关资源
        最近更新 更多