尝试分配一个数字并使其递增,但也保存程序关闭后使用的最后一个数字



我正在尝试在我的代码中为一个人分配一个数字。我正在创建一个程序来存储竞争对手的信息以进行竞争。我将竞争对手的信息存储在.txt文件中,以便在程序关闭时保留它。我希望能够为每个竞争对手分配一个竞争对手编号并让他们增加。他们还必须保存程序中使用的最后一个号码,并将每个特定号码保存给每个竞争对手。这样,在我输入了几个竞争对手并关闭程序后,当我重新启动程序时,提供给我输入信息的下一个竞争对手的竞争对手编号再次不是 1。我目前正在手动输入竞争对手编号。- 以下代码段是摘录,因为程序很长 - 我已经尝试过定期增加值之类的事情,但这并没有保存。谢谢

private static void writeToCompetitorFile()
{
    using (StreamWriter writer = new StreamWriter("../../CompetitorFile.txt", true))
    {
        writer.Write(CompetitorName + ",");
        writer.Write(CompetitorNumber + ",");
        writer.Write(CompetitorClimbOne + ",");
        writer.Write(CompetitorClimbTwo + ",");
        writer.Write(CompetitorClimbThree + ",");
        writer.WriteLine(CompetitorReactionTime);
        //This allows the data input by the user to be saved to a .txt file
    }
}

private static void SortData1()
{
    Console.Clear();
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("COMPETITOR DETAILSn");
    DataTable table = new DataTable();
    String[] reportLine = new String[6];
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Competitor Number", typeof(int));
    table.Columns.Add("Climb One Time", typeof(int));
    table.Columns.Add("Climb Two Time", typeof(int));
    table.Columns.Add("Climb Three Time", typeof(int));
    table.Columns.Add("Reaction Time", typeof(double));
    StreamReader srcnRdr = new StreamReader("../../CompetitorFile.txt");
    String Data = srcnRdr.ReadLine();
    //This adds all input information into the table to be displayed
    while (Data != null)
    {
        reportLine = Data.Split(',');
        table.Rows.Add(reportLine[0], reportLine[1], reportLine[2], reportLine[3], reportLine[4], reportLine[5]);
        Data = srcnRdr.ReadLine();
    }
    srcnRdr.Close();
    table.DefaultView.Sort = "Name";
    DataView viewtable = table.DefaultView;
    Console.WriteLine("=== Sorted by Name ===");
    for (int i = 0; i < viewtable.Count; i++)
    {
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}",
                viewtable[i][0],
                viewtable[i][1],
                viewtable[i][2],
                viewtable[i][3],
                viewtable[i][4],
                viewtable[i][5]);
        //This allows the user to view competitor data sorted by names in alphabetical order if they press '2' on the menu page
    }
}

查理,因为您已经将数据读入了数据表,因此只需按"竞争对手编号"对其进行排序,然后选择最后一个数字并将其递增1即可。

如果我回答了你的问题,请告诉我。

最新更新