c# - 按字母顺序排列数组

标签 c# arrays

所以我的代码遇到了一些麻烦。对于初学者,我遇到了输出所有数组的问题。请注意,我只编码了 12 天,由于我大学的学习观,我的老师略过了 C# 编码的基础知识。而且我刚刚了解到它不会按字母顺序排列它们......

    static int inputPartInformation(string[] pl)
    {
        int i = 0;

        do
        {
            Console.Write("Enter a Name: ");
            //for the player
            pl[i] = Console.ReadLine();

        }
        while (pl[i++].CompareTo("Q") != 0);
        //if they write Q for the player it will quit
        return i - 1;
    }

    static void Main(string[] args)
    {
        String[] players = new String[100];
        Array.Sort(players);
        // Sort array.

        //defines players in this new instance

        var count = inputPartInformation(players);

        //for the portion of the code that handles the input

        //calculates the average score

        Console.WriteLine("List of People in Order: {0}, {1}, {2}, {3}, {4}, {5}, {6},", players);
        Console.ReadLine();
    }
}

最佳答案

  • 您在填充名称之前进行排序;那什么都不做。
  • 您正在使用参数引用 {0}、{1}、{2} 等的固定多项列表打印单个项目;它不会起作用,即使它起作用也会将您的输出限制为前七项
  • 您不知道要分类多少项目。更改 void inputPartInformation(string[] pl) 以返回 count(即 i-1),并使用 Array.Sort(玩家, 0, 计数);

将多个字符串转换为单个字符串的最简单方法是使用 string.Join:

Console.WriteLine("List of People in Order: {0}", string.Join(", ", players.Take(count)));

关于c# - 按字母顺序排列数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979571/

相关文章:

java - 如何根据用户输入的位字符串用 1's and 0' 填充数组

arrays - 从indexpath.row中的数组获取特定值

c# - 从字符串数组列表转换为对象列表

c# - 浏览器后退按钮不填充 MVC 应用程序中的下拉值

c# - 轻量级 3D 图形引擎 .NET(紧凑且完整的框架)

c# - 将 JSON 反序列化为 C#

c# - MVC 中的 Azure Blob 存储安全选项

c# - 并发与正常收集?

javascript - For 循环运行数组的长度,但显示一个元素数组。length 次

jquery - 将类添加到数组中的第一个元素