- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试根据从文本文件中读取的玩家用户名和分数,在 C# Windows 窗体中创建前 10 名排行榜。
例如文本文件中的行数:
丹娜~21
到目前为止,这是我的代码:
private void scrnLeaderboard_Load(object sender, EventArgs e)
{
string[] players = FileMethods.ReadLines();
// Create List<KeyValuePair> to hold all player usernames and scores
List<KeyValuePair<int, string>> playerNamesScores = new List<KeyValuePair<int, string>>();
foreach (var item in players)
{
string[] playerDetails = item.Split('~');
if (playerDetails.Length == 2)
// Player's username and score added to List<KeyValuePair> playersNamesScores
// Key is score, Value is username
playerNamesScores.Add(new KeyValuePair<int, string>(Convert.ToInt32(playerDetails[1]), playerDetails[0].ToString()));
}
// Sorting the scores in descending order
var sortedScores = playerNamesScores.OrderByDescending(x => x).ToList<KeyValuePair<int, string>>();
// Assigning the appropriate values to each label's text on the leaderboard
lblPos1.Text = String.Format("{0}: \t{1}", sortedScores[0].Value, sortedScores[0].Key);
lblPos2.Text = String.Format("{0}: \t{1}", sortedScores[1].Value, sortedScores[1].Key);
lblPos3.Text = String.Format("{0}: \t{1}", sortedScores[2].Value, sortedScores[2].Key);
lblPos4.Text = String.Format("{0}: \t{1}", sortedScores[3].Value, sortedScores[3].Key);
lblPos5.Text = String.Format("{0}: \t{1}", sortedScores[4].Value, sortedScores[4].Key);
lblPos6.Text = String.Format("{0}: \t{1}", sortedScores[5].Value, sortedScores[5].Key);
lblPos7.Text = String.Format("{0}: \t{1}", sortedScores[6].Value, sortedScores[6].Key);
lblPos8.Text = String.Format("{0}: \t{1}", sortedScores[7].Value, sortedScores[7].Key);
lblPos9.Text = String.Format("{0}: \t{1}", sortedScores[8].Value, sortedScores[8].Key);
lblPos10.Text = String.Format("{0}: \t{1}", sortedScores[9].Value, sortedScores[9].Key);
}
FileMethods.ReadLines() 就是这样的:
string filepath = @"previousPlayers.txt";
string[] players = File.ReadLines(filepath).ToArray();
return players;
每次我编译代码时,我都会得到这个错误,'至少一个对象必须实现 IComparable。',在这条线上:
var sortedScores = playerNamesScores.OrderByDescending(x => x).ToList<KeyValuePair<int, string>>();
我不确定这意味着什么,或者我的代码如何工作。
非常感谢任何帮助!
最佳答案
OrderByDescending
方法按键对项目进行排序,由键选择器(此方法的第一个参数)返回。然后,它使用单个项目的这些键将一个项目与另一个项目进行比较,以决定哪些项目先出现,哪些出现在后。默认情况下,它使用 IComparable
的方法用于此比较的项目实现的接口(interface)。但是你指定了 x => x
作为键选择器,它返回 KeyValuePair<int, string>
类型的对象,它没有实现 IComparable
.因此无法对它们进行排序,您会收到“至少一个对象必须实现 IComparable”错误。
因为您只想按分数(类型 int
)和 int
来订购商品工具 IComparable
,您可以简单地使用返回分数的选择器:
var sortedScores = playerNamesScores.OrderByDescending(x => x.Key).ToList<KeyValuePair<int, string>>();
如果需要更复杂的项目排序方式(例如,您需要将分数相同的游戏按字母顺序排序),您可以声明实现 IComparer<KeyValuePair<int, string>>
的类接口(interface)并将其实例作为 OrderByDescending
的第二个参数传递方法。
关于C# Windows 窗体 : Using List<KeyValuePair> to create a Top 10 Leaderboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371037/
程序集,masm 嘿,我写了宏来打印存储在 dane1 段中的 1 字节值。 我将值除以 16,然后将提醒推送到堆栈,直到值==0。然后我弹出提醒将它们转换为 ASCII 码,并打印它们。 有人可以看
我在 Apache Geronimo 2.1.3 上有一个 Web 服务应用程序。 我正在使用 JAX-WS,使用 SOAP 1.1 注释 POJOS。 (使用 Sun JDK 1.5) 各种客户端都
我有一个数据变量,monthArray,它被多个消费者读取,并由单个定期更新程序线程定期更新数据。全部异步。 我已经考虑了这两个选项来安全地执行更新。 ArrayList tempArray
我有一组 3D 体。每个 Body 由 8 个点定义,每个点具有三个坐标。所有的物体都是立方体的或近似立方体的。我想用系统的点栅格“填充”立方体。坐标存储在简单的 data.frames 中。 我开发
我正在处理遗留代码,需要打补丁。 问题:一个古老的应用程序发送错误的 HTTP POST 请求。其中一个参数未经过 URL 编码。我知道这个参数总是排在最后而且我知道它的名字。我现在正尝试在运行在 t
我想在触摸屏幕时移动 sprite body ,但它不能发生...... -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
这个问题在这里已经有了答案: Can a union be initialized in the declaration? (3 个答案) 关闭 7 年前。 如果它是一个struct那么它就可以完成
我正在尝试获取生成 sigsys 信号的系统调用的地址!但我从 gcc 收到以下错误: gcc emulator.c -fms-extensions error: ‘siginfo_t’ has no
当我使用 Postman 进行 API 调用时,我收到一个 JSON 对象..这是我所期望的。 但是,当我像这样与 Guzzle 进行相同的调用时: $client = new \GuzzleHttp
在编码时,出现了差异。通常在编写简单的方法或构造函数时,我经常使用表达式体技术。但是,当我产生以下内容时: public class Sample : ISample { private r
我正在使用 LibGDX 创建一个新项目。 我想做的是,我将 tmx 文件中的主体加载到工作正常的关卡中。尸体也有一个 Sprite 。 问题是,我想让用户触摸场景中的某些 body 。当他们触摸 b
我的意图:在不使用 union 的情况下循环遍历一个结构的 30 个成员,所有成员都是字符数组类型,每个成员都存储对 itoa 的调用结果。在下面的代码中,我将结构体成员命名为a-z、A-D。在调用函
我必须将我的代码段之一从 C 转换为 java。代码如下。 union commandString{ char commndStr[20]; struct{ char
#include union NumericType { float value; int intvalue; }Values; int main() { Values.va
我在此代码中收到错误: fun num(num:Int):Int { if (num > 0){ print(num % 10) return num / 10
我是一名优秀的程序员,十分优秀!