使用 switch 计算元音 [英] Counting vowels using switch
问题描述
我试图设计一个计算句子中元音的程序.
I tried to design a program which counts the vowels in a sentence.
在我的代码中,我使用了 foreach
语句和 if/else if
语句.我想使用 switch
语句转换这些代码行但我不确定该去哪里.我需要添加一个新方法吗?我会很感激你的帮助.
In my code, I used a foreach
statement with the if/else if
statement. I would like to convert these line of code using the switch
statement
but I'm not really sure where to go. Do I need to add a new method? I would appreciate your help.
这是我到目前为止所尝试的:我检查了这个是非常错误的.例如 case 1
需要有一个常量.我不确定我在这里使用什么常量.
This is what I tried so far: I checked this one is very wrong. The case 1
for example needs to have a constant. I'm not sure what constant shall I use here.
foreach (char v in yourSentence)
{
switch (v)
{
case 1:
(v==ch1);
counta++;
j++;
break;
case 2:
(v==ch2);
counte++;
j++;
break;
case 3:
(v==ch3);
counti++;
j++;
break;
case 4:
(v==ch4);
counto++;
j++;
break;
case 5:
(v==ch3);
counti++;
j++;
break;
}
}
另一个问题:我试图改变listBox中显示文本的颜色.有可能有不同的颜色吗?我还在这里尝试的是前 5 个 (listBox1.Items.Add
) 是紫色的.而 (listBox1.Items.Add
) 的总和是蓝色的.但似乎并没有改变.我在这里错过了什么吗?
Another question: I tried to change the color of the display text in the listBox. Is that possible to have different colors? What I also tried here is the first 5 (listBox1.Items.Add
) are violet. And the sum of the (listBox1.Items.Add
) is blue. But it seems that it didn't change. Did I miss something here?
private void btnCount_Click(object sender, EventArgs e)
{
string yourSentence;
yourSentence = textBoxVowels.Text.ToLower().Trim();
char ch1 = 'a';
char ch2 = 'e';
char ch3 = 'i';
char ch4 = 'o';
char ch5 = 'u';
int counta = 0;
int counte = 0;
int counti = 0;
int counto = 0;
int countu = 0;
int j = counta + counte + counti + counto + countu;
foreach (char v in yourSentence)
{
if (v == ch1) { counta++; j++; }
else if (v == ch2) { counte++; j++; }
else if (v == ch3) { counti++; j++; }
else if (v == ch4) { counto++; j++; }
else if (v == ch5) { countu++; j++; }
}
listBox1.Items.Add("There are " + counta.ToString().Trim() + " a's in the sentence");
listBox1.Items.Add("There are " + counte.ToString().Trim() + " e's in the sentence");
listBox1.Items.Add("There are " + counti.ToString().Trim() + " i's in the sentence");
listBox1.Items.Add("There are " + counto.ToString().Trim() + " o's in the sentence");
listBox1.Items.Add("There are " + countu.ToString().Trim() + " u's in the sentence");
listBox1.Font = new Font("Arial", 12, FontStyle.Bold);
listBox1.ForeColor = Color.Violet;
listBox1.Items.Add("There are " + j.ToString().Trim() + " vowels in the sentence");
listBox1.ForeColor = Color.Blue;
}
private void btnClear_Click(object sender, EventArgs e)
{
textBoxVowels.Text = null;
listBox1.Items.Clear();
}
推荐答案
我想你误解了switch/case"的工作原理.只需将它们更改为:
I think you misunderstood how 'switch/case' works. Just change them to this:
case 'a':
counta++;
j++;
break;
Case 需要一个常量值来与当前字符进行比较.
Case needs a constant value that should be compared to the current character.
这篇关于使用 switch 计算元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!