【问题标题】:Creating a highscore list in c#在 C# 中创建一个高分列表
【发布时间】: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 =&gt; 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 &lt; 10; Score++) { textBox1.Text = new System.Windows.Forms.TextBox(); } } }
  • 这是我今天早些时候尝试的代码,但出现错误:无法将类型隐式转换为字符串

标签: c# arrays list integer


【解决方案1】:

让我们假设你有 20 名玩家,每个人都有一些分数。

当您决定创建一个 ints 数组来存储所有分数时,我也是如此:

int[] scores = new int[20];

例如,我用从 10 到 100 的随机唯一分数填充该数组。您按照自己的方式填写:

int[] scores = new int[20];
Random rnd = new Random();

for (int i = 0; i < scores.Length; i++)
{
    int score = 0;

    // This loop used to generate new random int until it wouldn't be unique (wouldn't exist in array)
    while (score == 0 || scores.Contains(score))
        score = rnd.Next(10, 100);

    scores[i] = score;

    lblAllScores.Text += "Player #" + (i + 1) + " score: " + scores[i] + "\n";
}

我将每个分数和玩家编号(作为 迭代次数 + 1)附加到 Label 上的 Form 以显示所有玩家及其分数。像这样的:

现在你(和我)想要获得前 10 名(显然是按照降序,从高到低)。这里的关键词是TakeDescending order。在 C# 中,通过 System.Linq 命名空间提供了诸如 TakeOrderByDescending 这样的漂亮扩展方法。如果我们使用得当,它们似乎可以给我们想要的结果。

我(和你)创建了一个新的ints 数组来仅存储前 10 名分数。然后获取初始数组scores 并使用OrderByDescending 方法将其中的值从最高到最低排序。 (x =&gt; x) 提供作为选择器来确定键,通过哪种方法对值进行排序——在我们的例子中是值本身。然后我使用Take(10) 方法从该排序数组中获取前 10 个得分 - 这是我们的前 10 个得分:

int[] top10Scores = scores.OrderByDescending(x => x).Take(10).ToArray();

而我需要做的就是将它放入Form,我可以通过使用for 循环对top10Scores 数组进行简单迭代来完成。但我(可能还有你)想知道什么 Player 有这个分数。上面我们使用(数组索引+1)作为玩家编号。我们知道每个玩家的分数是唯一的。现在我们可以从top10Scores 数组中获取分数值,并尝试使用Array.FindIndex 方法在源scores 数组中找到它的索引:

for (int i = 0; i < top10Scores.Length; i++)
{
    int playerNumber = Array.FindIndex(scores, score => score == top10Scores[i]) + 1;
    lblTop10Scores.Text += "Player #" + playerNumber + " score: " + top10Scores[i] + "\n";
}

在我们的表单中,我们有这个:

完整代码:

private void ScorePlayers()
{
    int[] scores = new int[20];
    Random rnd = new Random();

    for (int i = 0; i < scores.Length; i++)
    {
        int score = 0;

        while (score == 0 || scores.Contains(score))
            score = rnd.Next(10, 100);

        scores[i] = score;

        lblAllScores.Text += "Player #" + (i + 1) + " score: " + scores[i] + "\n";
    }

    int[] top10Scores = scores.OrderByDescending(x => x).Take(10).ToArray();

    for (int i = 0; i < top10Scores.Length; i++)
    {
        int playerNumber = Array.FindIndex(scores, score => score == top10Scores[i]) + 1;
        lblTop10Scores.Text += "Player #" + playerNumber + " score: " + top10Scores[i] + "\n";
    }
}

请注意,这只是一个草图和示例,用于演示如何使用 OrderByDescendingTake 扩展方法来获取从最高到最低值排序的前 10 个元素。在实际应用中,您可能至少会有简单的Dictionary&lt;string, int&gt;,其中将存储玩家姓名(如string)和他的得分(如int)。或者,更可能的是,它是一个名为 Player 的模型类,具有 NameScore 属性,每个 Player 将存储在一些 List&lt;Player&gt; 中:

public class Player
{
    public string Name { get; set; }
    public int Score { get; set; }
}

希望你能找到你的解决方案,这个例子会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 2015-03-04
    • 2021-08-16
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多