【发布时间】:2018-11-17 10:00:36
【问题描述】:
我是一所大学的一年级学生,该大学非常注重自学 (SCRUM),没有任何经典课程。 正因为如此,我基本上从这样的网站上学到了我所知道的一切。 我不想养成任何坏习惯或错误的理解。 那么,这段代码有用吗? 我不是在寻找优化(虽然不介意任何提示;)),因为我会随着时间的推移而学习,而是在总体结构和我的思维方式是否正确的情况下。
static void Main(string[] args)
{
string repeat;
//do while loop for if the user wants to run the program again
do
{
//asigns variables
string text;
int vowels, consonants, numbers, otherSymbols;
var hsVowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var hsConsonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
var hsNumbers = new HashSet<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
//asks for input
Console.WriteLine("Input anything and the program wil tell you how many vowels, consonants, numbers and other symbols you gave.");
text = Console.ReadLine().ToLower();
//calculates
vowels = text.Count(c => hsVowels.Contains(c));
consonants = text.Count(c => hsConsonants.Contains(c));
numbers = text.Count(c => hsNumbers.Contains(c));
otherSymbols = text.Length - (vowels + consonants + numbers);
//shows the result
Console.WriteLine("Your input has {0} vowels, {1} consonants, {2} numbers and {3} other Symbols.", vowels, consonants, numbers, otherSymbols);
//asks if the user wants to run the program again
Console.WriteLine("Would you like to try again? (yes/no)");
repeat = Console.ReadLine();
//tests if the users input was valid (yes/no)
while (!(repeat.ToLower().Contains("yes") || repeat.ToLower().Contains("no")))
{
Console.WriteLine(@"Invalid input. Please answer ""yes"" or ""no"" .");
repeat = Console.ReadLine();
}
} while (repeat.ToLower().Contains("yes"));
}
不知何故,我无法让代码示例将我的代码识别为 C#。 如果有人能告诉我怎么做,将不胜感激!
【问题讨论】:
-
虽然您可能会在这里得到一些回应,但 Stack Exchange 有一个专门针对此的网站,您可能会在该网站上获得更好的结果:codereview.stackexchange.com
-
@sellotape 感谢您的推荐!一旦 40 分钟的冷却时间结束,我会这样做。如果有人回复,我应该删除这个问题还是留着?
-
我可能会删除它 - 人们总是可以回复新的。
-
您无法删除此问题,因为其他人已投入时间和精力来回答它。亚历克斯的回答可能对其他人也有帮助,所以我想没关系...