gpt4 book ai didi

C# 将一个列表的值除以另一个列表的值以生成一个新列表

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:27 25 4
gpt4 key购买 nike

我希望将每个列表的相应值进行划分,以生成一个新列表,其中包含我的两个列表中每个相应索引的划分结果。我的两个列表都是 8 个值长并且都是 int 列表。我目前正在创建两个要这样划分的列表:

private void StartSchedule_Click(object sender, EventArgs e)
{
string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\A2 Computing\C# Programming Project\TriHard.accdb";
string SelectQuery = "SELECT Time.AthleteID, Athlete.AthleteName, Time.EventTime, Event.EventDistance FROM Event INNER JOIN (Athlete INNER JOIN [Time] ON Athlete.[AthleteID] = Time.[AthleteID]) ON Event.[EventID] = Time.[EventID];";
OleDbConnection Connection = new OleDbConnection(ConnectionString);
OleDbCommand Command = new OleDbCommand(SelectQuery, Connection);
Command.Connection.Open();

OleDbDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);

PaceCalculator pace = new PaceCalculator();

List<int> Distancelist = new List<int>();
List<int> Secondslist = new List<int>();
List<int> Pacelist = new List<int>();
while (Reader.Read())
{
pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
int DistanceInt = Convert.ToInt32(pace.Distance);
Distancelist.Add(DistanceInt);

pace = new PaceCalculator();
pace.Time = (string)Reader["EventTime"]; //Reads in EventTime
double Seconds = TimeSpan.Parse(pace.Time).TotalSeconds; //Converts the string into HH:MM:SS as a double
int SecondsInt = Convert.ToInt32(Seconds); //Converts the double into an integer, returning the seconds in the total time
Secondslist.Add(SecondsInt); //Adds the Seconds for each time to the list;

//Need to fix this currently returns just 0
var Pacelist2 = PaceCalc(Distancelist, Secondslist);
listBox3.DisplayMember = "PaceInt";
listBox3.DataSource = (Pacelist2);



}


listBox1.DisplayMember = "DistanceInt";
listBox1.DataSource = Distancelist;

listBox2.DisplayMember = "SecondsInt";
listBox2.DataSource = Secondslist;

这是我正在调用的函数,它试图划分列表,但似乎不起作用:

public List<int> PaceCalc(List<int> Dlist, List<int> Slist)
{
PaceCalculator pace = new PaceCalculator();
List<int> Plist = new List<int>();

pace = new PaceCalculator();
for (int i = 0; i == Dlist.Count; i++)
{
int PaceInt = Dlist[i] / Slist[i];
Plist.Add(PaceInt);
}
return Plist;
}

我想在 listBox3 中显示除法的结果。我是否正确划分列表以及如何在列表框中显示它?

最佳答案

您的 for 循环永远不会执行,因为您正在测试 i == Dlist.Count。应该是:

for (int i = 0;i < Dlist.Count; i++)

或者,您可以使用 LINQ 执行此操作:

public List<int> PaceCalc(List<int> Dlist, List<int> Slist)
{
return Dlist.Zip(Slist, (a, b) => a / b).ToList();
}

关于C# 将一个列表的值除以另一个列表的值以生成一个新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com