c#确定文本是辅音,元音,数字还是其他字符。然后它显示每个的计数。 [英] c# determines if the text is a consonant, vowel, digit or other character. Then it displays the count of each.

查看:173
本文介绍了c#确定文本是辅音,元音,数字还是其他字符。然后它显示每个的计数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让代码计入输入文本框中的元音。我做错了什么?



I cannot get me code count the vowels in the entry text box. What have I done wrong?

string inputString;
inputString = this.entryTextBox.Text.ToLower();


char[] vowels = new char[] {'a', 'e', 'i', 'o', 'u'};
string vow = new string(vowels);

for (int index = 0; index < inputString.Length; index++)
{

    if (char.IsLetterOrDigit(inputString[index]))
    {
        if (inputString.Contains(vow))
                vowelCount++;

    }
    else if (char.IsDigit(inputString[index]))
       digitCount++;

}
    this.voweldisplayLabel.Text = vowelCount.ToString();
    this.digitsdisplayLabel.Text = digitCount.ToString();

推荐答案

显然,您的错误是使用 inputString.Contains 。它检查是一个字符串是另一个字符串的子字符串,但你的逻辑需要检查是单个字符是元音之一。此外,变量 vow 的计算完全没有意义,而且您的包含的调用只是重复 inputString.Length 次,给出相同的结果。



相反,如果找到,你应该检查一下inputString [index] in 元音。这是最简单的方法之一:

http:// msdn .microsoft.com / zh-CN / library / 7eddebat.aspx [ ^ ]。



例如:

Obviously, your bug is using inputString.Contains. It checks up is one string is a sub-string of another one, but your logic requires checking up is a single character is one of the vowels. Moreover, the calculation of the variable vow is completely pointless, and your call of Contains is just repeated inputString.Length times, giving the same result.

Instead, you should check up if you find inputString[index] in vowels. This is one of the simplest ways to do it:
http://msdn.microsoft.com/en-us/library/7eddebat.aspx[^].

For example:
if (System.Array.IndexOf(vowels, inputString[index]) >= 0)
   ++vowelCount;



-SA


你可以用这个:

..............................



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;



命名空间ConsoleApplication1

{

class program

{

static void Main(string [] args)

{

int i,conso = 0,count = 0;

String letter;

letter = Console.ReadLine ();

for(i = 0;我< letter.Length;如果(字母[i] =='a'|| letter [i] =='e'| | letter [i] =='i'|| letter [i] =='o'|| letter [i] =='u'|| letter [i] =='A'|| letter [i] = ='E'|| letter [i] =='我'|| letter [i] =='O'|| letter [i] =='U')

{

++ count;

Console.WriteLine(letter [i] +是Vowel);

}

else

{

++ conso;

Console.WriteLine(letter [i] +是辅音);

}



}

Console.WriteLine(元音总数为:+ conso);

Console.WriteLine(元音总数:+计数);

}

}

}
You can use this:
..............................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i, conso = 0, count = 0;
String letter;
letter=Console.ReadLine();
for (i = 0; i < letter.Length; i++)
{

if (letter[i] == 'a' || letter[i] == 'e' || letter[i] == 'i' || letter[i] == 'o' || letter[i] == 'u' || letter[i] == 'A' || letter[i] == 'E' || letter[i] == 'I' || letter[i] == 'O' || letter[i] == 'U')
{
++count;
Console.WriteLine(letter[i]+" is Vowel");
}
else
{
++conso;
Console.WriteLine(letter[i]+" is Consonant ");
}

}
Console.WriteLine("Total Number of vowels are : " + conso);
Console.WriteLine("Total Number of vowels are : " + count);
}
}
}


string inputString;
inputString = this.entryTextBox.Text.ToLower();

char[] vowels = {'a', 'e', 'i', 'o', 'u'};
string vow;
int vowelCount = 0;
int digitCount = 0;


for int j = 0; j < 6; j++)
{
    vow = vow + vowels[j];
}

for (int index = 0; index < inputString.Length; index++)
{

    if (char.IsLetterOrDigit(inputString[index]))
    {
        if (inputString.Contains(vow))
                vowelCount++;

    }
    else if (char.IsDigit(inputString[index]))
       digitCount++;

}
    this.voweldisplayLabel.Text = vowelCount.ToString();
    this.digitsdisplayLabel.Text = digitCount.ToString();





所以你有,你的代码,翻新......或类似的......



So there you are, your code, refurbished ... or something like that ...


这篇关于c#确定文本是辅音,元音,数字还是其他字符。然后它显示每个的计数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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