所以我的代码遇到了一些麻烦。对于初学者,我遇到了输出所有数组的问题。请注意,我只编码了 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/