希望有人能够帮忙。我创建了一个可变长度的数组,可以接受多个名称输入。我现在想按字母顺序对数组进行排序,并将其返回到控制台屏幕上。我认为Array.Sort(names)会为我完成这个操作,但是我遇到了异常。我已经查看了笔记、示例和在线资源,但似乎没有与我做的事情相匹配的内容。到目前为止,我的代码如下。我快要疯了!顺便说一下,我已经试图解决了这个问题,但是我无法做到,所以我需要有人解释我哪里错了,因为我是30多岁自学编程的人,今天是星期天,我正在努力工作,手头没有笔记来准确地覆盖这个问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Student_Array
{
class Program
{
struct Student
{
public string Name;
}
static void Main(string[] args)
{
int numberOfStudents;
Student[] names;
string input;
Console.WriteLine("How many students are there?");
input = Console.ReadLine();
numberOfStudents = int.Parse(input);
names = new Student[numberOfStudents];
for (int i = 0; i < names.Length; i++)
{
Student s;
Console.WriteLine("Please enter student {0}'s name", (i + 1));
s.Name = Console.ReadLine();
names[i] = s;
}
***Array.Sort<Student>(names);***
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i].Name);
}
}
}
}