【发布时间】: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#