首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试分配一个数字并使其递增,但也保存程序关闭后使用的最后一个数字。

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

Stack Overflow用户
提问于 2019-01-07 12:02:56
回答 1查看 47关注 0票数 0

我试图在我的代码中给一个人分配一个号码。我正在创建一个程序,存储竞争对手的信息。我将竞争对手的信息存储在一个.txt文件中,以便在程序关闭时保存它。我想要能够分配一个竞争对手号码给每个竞争对手,并让他们增值。他们还必须保存程序中使用的最后一个号码,并将每个特定号码保存给每个竞争者。这样,在我输入了几个竞争者并关闭程序之后,给下一个竞争者输入信息的竞争者编号,当我重新启动程序时,不再是1。我目前正在手动输入竞争对手的号码。下面的代码片段是提取出来的,因为程序很长--我尝试过定期递增值,但这是不保存的。谢谢

代码语言:javascript
复制
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
    }
}
代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-01-07 12:18:59

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54074071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档