C# finding the shortest and longest word in a array

后端 未结 8 1252
小蘑菇
小蘑菇 2021-01-15 04:32

I am trying to find the shortest and longest string value based on length and im getting stuck. As of now the script exits after the writeline. I think the code needs some h

8条回答
  •  借酒劲吻你
    2021-01-15 05:07

    As @DStanley and @RenniePet pointed out, your original code doesn't exactly do what you're trying to accomplish. I do believe you wanted to cycle through the member of your string array and fill them with console inputs. A working code would look like this:

        string[] word = new string[5];
    
            for (int i = 0; i < word.Length; i++)
            {
                Console.Write("Input text: ");
                word[i] = Console.ReadLine();
            }
    
            List _sortedWords = word.ToList();
            _sortedWords.Sort((x, y) => x.Length.CompareTo(y.Length));
    
            Console.WriteLine("Smaller: " + _sortedWords[0]);
            Console.WriteLine("Larget: " + _sortedWords[_sortedWords.Count-1]);
    
            Console.ReadLine();
    

提交回复
热议问题