【发布时间】:2021-11-30 09:34:08
【问题描述】:
我需要使用数组/列表来完成我的作业,并考虑在我的 Windows 形式游戏的高分列表中使用一个。
现在我正在为我的 Score 使用常规 int,我想知道如何将它放入我的 highscore.cs 上的高分列表的数组中
game.cs 中得分的当前代码
int Score = 0;
Scorelabel.Text ="score:" + score;
if (Picturebox.Left < -180)
{
Picturebox.Left = 800;
Score++;*
我尝试过的事情:
namespace Game
{
public partial class Highscore : Form
{
public Highscore()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int[] Highscore = new int[10];
for (int Score = 0; Score < 10; Score++)
{
textBox1.Text = new System.Windows.Forms.TextBox();
}
}
}
我试图将初始分数设为一个数组。我最终会得到错误:“Operator ++ cannot be applied to operand of type 'int[]'
还尝试将我的 int 放入数组,但会收到无法将 int 转换为数组的错误。
我想要什么
我希望我在 game.cs 中的分数显示在我的 highscore.cs 上的一个列表框中,该列表显示前 10 个分数从最高到最低。
【问题讨论】:
-
Scorelabel.Text = $"score: {string.Join(", ", score.OrderByDescending(x => x).Take(10))};" -
您能否展示您尝试过的代码而不仅仅是描述?
-
没有看到你之前尝试过的东西,我不能说 100% 是什么导致了除了假设之外的错误。高分列表通常会随着越来越多的用户玩它们而增长,因此数组没有多大意义,因为它们(至少在 C# 中)必须声明它们的大小并在事实并非它们真正的工作方式之后进行修改。如果要修改大小,我建议使用强类型列表而不是数组。尝试您之前对阵列所做的尝试,如果您仍有问题,请告诉我们。备份我的声明:stackoverflow.com/questions/4840802/change-array-size
-
namespace Game { public partial class HighscorePage : Form { public HighscorePagina() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { int[] Highscore = new int[10]; for (int Score = 0; Score < 10; Score++) { textBox1.Text = new System.Windows.Forms.TextBox(); } } } -
这是我今天早些时候尝试的代码,但出现错误:无法将类型隐式转换为字符串