如何计算int列表中每个数字的频率?

时间:2019-07-19 10:24:21

标签: c#

我有一个球员号码列表(国际比赛名单),我也有很多圈(国际比赛次数)。每当球员完成一圈时,我都必须在文本框中输入球员的号码。如果有效,则在文本框中输入的玩家编号将发送到列表中。但是,输入的每个球员编号不能超过圈数。

我是Windows窗体的新手,所以我只使用列表来解决问题,我已经看到人们使用字典来解决类似的问题,但是我不太确定如何将其合并来解决这个问题。 / p>

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

        bool result;
        int RunnerNumberInOrder;

        repeat2 = (getLaps2 * rows2);
        textBox2.Focus();

        //ReadPlayerNumber is the list that contains the list of the runners in the race. 
        //PlayerNumberInOrder int list is a global list and it is the list that the runners number order gets sent to if valid

        result = int.TryParse(textBox2.Text, out RunnerNumberInOrder); //checks if the value entered is an integer


        if (result == false || !ReadRunnerNumbers2.Contains(RunnerNumberInOrder)) //if the value entered is not an integer an error message will pop up and ask to enter a valid number
        {
            label3.Text = "Please enter a valid player number from the list";

        }

        else
        {
            if (RunnerOrder.Contains(RunnerNumberInOrder))
            {

                count++;
            }

            if (count == getLaps2)
            {
                count = 0;
                label3.Text = "max laps for this runner has been entered";


                invalid.Add(RunnerNumberInOrder); //invalid is an int list that I used to send the numbers of the runners that have completed all laps. 


                textBox2.Clear();
                return;

            }

        }

        if (invalid.Contains(RunnerNumberInOrder))
        {
            MessageBox.Show("Max number of laps for this runner has been entered");
            textBox2.Clear();
        }

        if (result == true && ReadRunnerNumbers2.Contains(RunnerNumberInOrder) && !invalid.Contains(RunnerNumberInOrder))
        {
            RunnerOrder.Add(RunnerNumberInOrder); //if number entered is valid, the it is added to the global variable             
            PlayerNumbersEntered++;
            label8.Text = RunnerNumberInOrder.ToString();//counts how many valid car numbers have been enterted
            textBox2.Clear();
        }


    }

我的代码有点有效,但是当圈数为3时,它有时只允许一个数字2倍,有时还允许输入多个圈数。我希望当玩家的号码等于圈数并且不应该再允许该数目时弹出错误消息。我还必须使用text_change。提前为帮助加油。

1 个答案:

答案 0 :(得分:0)

我认为在代码中看到的主要问题是count变量,它似乎是一个全局变量,但您似乎像对特定RunnerNumber的计数一样使用它。

按照我的看法,您可以通过创建一类具有数字ID和Count的Runner来解决此问题,或者甚至更简单地按如下方式获取该跑步者的数量:

specificRunnerCount = RunnerOrder.Where(x => x == RunnerNumberInOrder).Count()

使用此方法,您可以将代码修改为以下形式:

int specificRunnerCount = RunnerOrder.Where(x => x == RunnerNumberInOrder).Count();
if ((specificRunnerCount + 1) == getLaps2)  // +1 because we haven't added this number to the count yet.
{
    label3.Text = "max laps for this runner has been entered";
    invalid.Add(RunnerNumberInOrder); 
    textBox2.Clear();
    return;
}
// Continue code as is

此外,在代码中,您向无效列表添加值的代码部分:invalid.Add(RunnerNumberInOrder);,使用return语句结束该函数。因此,我认为您无需检查无效列表是否包含if (invalid.Contains(RunnerNumberInOrder))

中的跑步号码。

希望这对您有所帮助。