2013-07-21 54 views
0

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

    } 
    } 
+1

當然,你不想讓用戶輸入一個字母*每次迭代*。在開始循環之前,你不想問這個嗎? –

+0

是喬恩,我還在學習,謝謝 – DavMar

回答

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); 
    } 
} 
+0

如果您覺得通過添加'== true'可以使條件表達式讀得更好,那麼它應該作爲局部變量提取出來,併爲它指定一個名稱它的意義。但是,在這種情況下,'word.Contains(searchChar)'讀取得很好,沒有'== true'。 –