0

C# 中的新功能,我必须为以下内容编写一个控制台应用程序。

用户可以输入他的单词,单词被存储到一个数组中,

提示用户输入一个字符,该字符将检索所有具有该字符的单词。我不知道如何在 if 语句中设置条件以及如何使用用户输入来检索单词。这是我的尝试代码:

int WCount;
string LargestWord = " ";
string SmallestWord = " ";
int vowelcount = 0;

List<string> wordsarr = new List<string>();
Console.WriteLine("How many words are you going to enter?");
WCount = int.Parse(Console.ReadLine());

for (int j = 0; j < WCount; j++)
{
  Console.WriteLine("Please enter your word");
  wordsarr.Add(Console.ReadLine());
  LargestWord = wordsarr[0];
  SmallestWord = wordsarr[1];
  string vowel = wordsarr[j].ToString();

  if(LargestWord.Length<wordsarr[j].Length)
  {
     LargestWord = wordsarr[j];
  }
  else if (SmallestWord.Length>wordsarr[j].Length)
  {
     SmallestWord = wordsarr[j];
                        }
     Console.WriteLine("Please enter a letter: ");
     char userinput = char.Parse(Console.ReadLine());

     if (userinput == wordsarr[j])
     {

     }
   }
4

2 回答 2

2

我会做这样的事情:

Console.WriteLine("How many words are you going to enter?");
int wordCount = int.Parse(Console.ReadLine());

string[] words = new string[wordCount];
for (int i = 0; i < words.Length; i++)
{
  Console.WriteLine("Please enter your word");
  words[i] = Console.ReadLine();
}

Console.WriteLine("Please enter a letter: ");
string searchChar = Console.ReadLine();

for (int i = 0; i < words.Length; i++)
{
  string word = words[i];
  if (word.Contains(searchChar) == true)
  {
     Console.WriteLine(word);
  }
}
于 2013-07-21T16:49:50.587 回答
0

您可以使用在 wordsarr 上调用的方法 find wordsarr.Find() 请检查如何在此处使用 find 方法http://www.codeproject.com/Articles/388257/Csharp-Tips-Using-delegate-in-List-Find-predicate 检查是否为字符串包括给定的字符看这里如何检查字符串是否包含 C# 中的字符?

于 2013-07-21T16:34:58.663 回答