我试图在我的代码中给一个人分配一个号码。我正在创建一个程序,存储竞争对手的信息。我将竞争对手的信息存储在一个.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
}
}发布于 2019-01-07 12:18:59
因为您已经将数据读取到datatable中,所以只需按“竞争对手编号”对其进行排序,并选择最后一个数字并将其增加1。
如果我回答了你的问题,请告诉我。
https://stackoverflow.com/questions/54074071
复制相似问题