【问题标题】:Is the structure of my C# code and my way of thinking right?我的 C# 代码的结构和我的思维方式是否正确?
【发布时间】: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 分钟的冷却时间结束,我会这样做。如果有人回复,我应该删除这个问题还是留着?
  • 我可能会删除它 - 人们总是可以回复新的。
  • 您无法删除此问题,因为其他人已投入时间和精力来回答它。亚历克斯的回答可能对其他人也有帮助,所以我想没关系...

标签: c# console-application


【解决方案1】:

我已经测试了代码并且它可以工作。干得好:-)

正如 sellotape 建议的那样,在建议的网站上发帖可能是个好主意。

话虽如此,我对您的代码进行了一些更改,以使其更具可读性并减少重复:

在进入循环之前使用 while 循环并设置条件变量。你至少会做一个循环:

        string repeat = "yes";
        //Main loop
        while (repeat.Contains("yes")) 

单行变量声明。我个人不建议这样做。通过在一行中同时声明一个变量,它更易于阅读:

    string inputText;
    int howManyVowels;
    int howManyConsonants;
    int howManynumbers;
    int howManySymbols;

从上面你可以看到,我已经尝试尽可能明确地使用我的命名。命名时不要害怕冗长。一个命名良好的变量是不言自明的,并且表明了意图。

var 的使用。关于它有很多意见。在写好代码时,我个人认为 var 一定要尽量少用。在这种情况下就可以了,因为右手边是不言自明的。但是最好显示左侧类型。

List<char> listOfVowels = new List<char> { 'a', 'e', 'i', 'o', 'u' };

这只是我的偏好。使用 List 而不是 HashSet。由于我们并不真正关心性能 - 在这种情况下 - List 的使用突出了我们正在处理一个 char 列表这一事实。

最后一点,如果您需要检查小写响应,则在初始化时设置重复变量 ToLower():

repeat = Console.ReadLine().ToLower()

希望对你有帮助。

【讨论】:

  • 感谢您如此深入地回答!我确实学到了一些关于良好编程的重要基础知识。
猜你喜欢
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多