Forum Discussion
Frankduc
Jun 24, 2024Copper Contributor
c# How to loop and count back in another loop?
Hi, var Quodown = new List<int>() {9,7,6,9,7,6,4,3,0};
double sumi = 0;
double avg = 0;
for (int i = 8; i < Quodown.Count; i--)
{
sumi += Quodown[i];
Print("...
Arabidopsis
Aug 13, 2024Copper Contributor
Hi,
Let's take a look at what you've done.
You want to loop from the back of the list to the front, but i<Quoodown Count was used as a constraint for the for statement, so this loop obviously doesn't work properly. I have fixed the first loop for you, and it seems to work properly now.
var Quodown = new List<int>() { 9, 7, 6, 9, 7, 6, 4, 3, 0 };
double sumi = 0;
double avg = 0;
for (int i = 8; i > 0; i--)
{
sumi += Quodown[i];
Console.WriteLine("sumi: "+ sumi);
for (int y = i; y < 8; y++)
{
for (int z = Quodown.Count; z > 0; z--)
{
avg = sumi / z;
Console.WriteLine(z);
}
}
}Wishing you a pleasant programming experience!