通过 for 循环添加值后列表返回 0 - c#
List Returning 0 after Adding values through a for loop - c#
采用这种方法后,我的列表计数 return 0,但它不应该。所有调试都是正确的,它们都不是 null 或其他东西。我正在使用统一。
有谁知道问题出在哪里?
List<Coordinates> FillCoordinates()
{
List<Coordinates> coordinatesList = new List<Coordinates>();
Debug.Log(minLenght);
Debug.Log(maxLenght);
Debug.Log(heights.Count);
for (int i = minLenght; i > maxLenght; i++)
{
for (int j = 0; j < heights.Count; j++)
{
coordinatesList.Add(new Coordinates(i, heights[j]));
}
}
return coordinatesList;
}
坐标class:
public class Coordinates
{
public int lenght;
public float height;
public Coordinates(int lenght_, float height_)
{
lenght = lenght_;
height = height_;
}
}
我猜这永远不会执行,更改 i < maxLenght
for (int i = minLenght; i > maxLenght; i++)
{
for (int j = 0; j < heights.Count; j++)
{
coordinatesList.Add(new Coordinates(i, heights[j]));
}
}
@obl说的对,这个不会执行:
for (int i = minLenght; i > maxLenght; i++)
for 循环语句如下:
"Start with i
at minLength
, and while i
is greater than maxLength
, run the loop and then increment i
."
由于 i 不大于 maxLength,因此循环永远不会 运行。
改成这样:
for (int i = minLenght; i < maxLenght; i++)
"Start with i
at minLength
, and while i
is less than maxLength
, run the loop and then increment i
."
现在 运行 从 minLength
一直到 maxLength-1
。
你是对的,当 i
等于 maxLength
时,这不会 运行 最后一次循环。要解决这个问题(如果它真的是你想要的),只需像这样调整它:
for (int i = minLenght; i <= maxLenght; i++)
"Start with i
at minLength
, and while i
is less than or equal to maxLength
, run the loop and then increment i
."
采用这种方法后,我的列表计数 return 0,但它不应该。所有调试都是正确的,它们都不是 null 或其他东西。我正在使用统一。 有谁知道问题出在哪里?
List<Coordinates> FillCoordinates()
{
List<Coordinates> coordinatesList = new List<Coordinates>();
Debug.Log(minLenght);
Debug.Log(maxLenght);
Debug.Log(heights.Count);
for (int i = minLenght; i > maxLenght; i++)
{
for (int j = 0; j < heights.Count; j++)
{
coordinatesList.Add(new Coordinates(i, heights[j]));
}
}
return coordinatesList;
}
坐标class:
public class Coordinates
{
public int lenght;
public float height;
public Coordinates(int lenght_, float height_)
{
lenght = lenght_;
height = height_;
}
}
我猜这永远不会执行,更改 i < maxLenght
for (int i = minLenght; i > maxLenght; i++)
{
for (int j = 0; j < heights.Count; j++)
{
coordinatesList.Add(new Coordinates(i, heights[j]));
}
}
@obl说的对,这个不会执行:
for (int i = minLenght; i > maxLenght; i++)
for 循环语句如下:
"Start with i
at minLength
, and while i
is greater than maxLength
, run the loop and then increment i
."
由于 i 不大于 maxLength,因此循环永远不会 运行。
改成这样:
for (int i = minLenght; i < maxLenght; i++)
"Start with i
at minLength
, and while i
is less than maxLength
, run the loop and then increment i
."
现在 运行 从 minLength
一直到 maxLength-1
。
你是对的,当 i
等于 maxLength
时,这不会 运行 最后一次循环。要解决这个问题(如果它真的是你想要的),只需像这样调整它:
for (int i = minLenght; i <= maxLenght; i++)
"Start with i
at minLength
, and while i
is less than or equal to maxLength
, run the loop and then increment i
."