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

时间:2019-01-07 12:02:57

标签: c#

我正在尝试为我的代码中的人分配一个数字。我正在创建一个程序,用于存储竞争对手的竞争信息。我将竞争对手信息存储在.txt文件中,以便在程序关闭时保留它。我希望能够为每个竞争对手分配一个竞争对手编号,并使它们递增。他们还必须保存程序中使用的最后一个号码,并将每个特定的号码保存到每个竞争对手。这样,在我进入了几个竞争对手并关闭程序后,当我重新启动程序时,输入给下一个竞争对手的竞争对手编号即为我输入的信息。我目前正在手动输入竞争对手的号码。 -由于程序很长,以下代码是摘录的-我尝试了定期增加值等操作,但这并不能保存。谢谢

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
    }
}

1 个答案:

答案 0 :(得分:0)

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

请回答我的问题。