使用char来显示单词

时间:2013-07-21 16:28:44

标签: c# string char

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

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

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

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])
     {

     }
   }

2 个答案:

答案 0 :(得分: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);
  }
}

答案 1 :(得分:0)

你可以使用在wordsarr上调用的方法find     wordsarr.Find() Plsease检查如何在这里使用find方法http://www.codeproject.com/Articles/388257/Csharp-Tips-Using-delegate-in-List-Find-predicate 要检查字符串是否包含给定字符,请在此处查看How can I check if a string contains a character in C#?