首页 > 解决方案 > 尝试分配一个数字并使其递增,但还要保存程序关闭后使用的最后一个数字

问题描述

我正在尝试为我的代码中的一个人分配一个号码。我正在创建一个存储竞争对手信息以供比赛的程序。我将竞争对手信息存储在 .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 DETAILS\n");
    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
    }
}

标签: c#

解决方案


查理,因为您已经将数据读入数据表,所以只需按“竞争对手编号”对其进行排序,然后选择最后一个数字并将其增加 1。

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


推荐阅读