2015-06-02 136 views
-1

我試圖製作一個程序,讓用戶輸入3個名稱,然後按字母順序顯示,但我不確定如何以這種方式顯示字符串 任何幫助將不勝感激按字母順序顯示名稱

string firstName, secoundName, thirdName; 
     int myMoves = 1; 
     int myHolder; 

     Console.WriteLine("Please Enter The First Name: "); 
     firstName = Console.ReadLine(); 

     Console.WriteLine("Please Enter The Secound Name: "); 
     secoundName = Console.ReadLine(); 

     Console.WriteLine("Please Enter The Third Name: "); 
     thirdName = Console.ReadLine(); 

     do 
     { 
      if() 
      { 

      } 

     } while (myMoves > 0); 
+1

你有搜索的StackOverflow的答案? –

回答

1

而不是每個名稱的獨立變量,將名稱添加到可以本地排序的集合類型,List<string>

string[] prompts = new [] { "First", "Second", "Third" }; 
string tempName = null; 

// Create an empty collection of strings for the names, 
// starting off with a capacity equal to the number of prompts 
IList<string> names = new List<string>(prompts.Length); 

// Let's make the collection smarter so you don't need to repeat code 
foreach (string prompt in prompts) 
{ 
    Console.WriteLine("Please Enter The {0} Name: ", prompt); 

    // collect the name 
    tempName = Console.ReadLine(); 

    // check that something was entered before adding it to the list 
    if (!string.IsNullOrEmpty(tempName)) 
    { 
     names.Add(tempName); 
    } 

    // reset the temporary name variable; probably not necessary but... 
    tempName = null; 
} 

// Sort the list 
names.Sort(); // will do a default string comparison for ordering 

// Print the names 
foreach (string name in names) 
{ 
    Console.WriteLine(name); 
}