我试图让一个程序让用户输入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);
答案 0 :(得分: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);
}