我想在任何文字中找出元音。但不能 [英] i want to find out vowel in any text. but can't

查看:61
本文介绍了我想在任何文字中找出元音。但不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

        }
    }
}

推荐答案

您可以尝试此代码。< br $>


You can try this code.

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();
}


不要使用字符数组作为输入,因为你可以像这样直接创建一个字符串:

Don't use a character array for your input, as you can create a string directly like this:
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.


也可以试试..





try this too..


//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 = { '!','@',' ','


这篇关于我想在任何文字中找出元音。但不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆