我是罗德斯大学的一年级学生,作为家庭作业的一部分,我的任务是创建一个程序,创建一个名为sortAlpha的数组。它应该提示用户输入字母的任意五个字母并按字母顺序排列。经过这么多尝试,我无法使它发挥作用。
答案 0 :(得分:1)
这是一项非常简单的任务。您只需要在VS中创建一个简单的控制台项目。 并且需要遵循非常简单的步骤。
在控制台上编写已排序的数组数据。
static void Main(string[] args)
{
Console.WriteLine("Please Enter 5 characters:");
char[] data = new char[5];
for (int i = 0; i < 5; i++)
{
data[i] = Console.ReadKey().KeyChar;
}
Console.WriteLine("");
Array.Sort(data);
Console.WriteLine("Answer is:" );
foreach (var ch in data)
{
Console.Write(ch);
}
Console.Read();
}
答案 1 :(得分:0)
您使用冒泡排序来排列数组,但由于冒泡排序会对数字进行排序,因此您可以使用字符的unicode值。
Console.WriteLine("Enter five letter of the alphabet");
char[] sortAlpha = new char[5];
char temp;
//Getting the values from the user.
for (int i = 0; i < sortAlpha.Length; i++)
{
char[] input = Console.ReadLine().ToCharArray();
sortAlpha[i] = input[0];
}
//bubble sort.
for (int write = 0; write < sortAlpha.Length; write++)
{
for (int sort = 0; sort < sortAlpha.Length - 1; sort++)
{
//comparing the unicode values of the chars.
if ((int)sortAlpha[sort] > (int)sortAlpha[sort + 1])
{
temp = sortAlpha[sort + 1];
sortAlpha[sort + 1] = sortAlpha[sort];
sortAlpha[sort] = temp;
}
}
}
for (int i = 0; i < sortAlpha.Length; i++)
{
Console.WriteLine(sortAlpha[i]);
}
Console.ReadKey();
}