带集合输出的C#递归

2024-09-30 20:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用递归函数并输出结果值的列表。 这是我想出的密码,但它给了我

Error (CS0161): 'Script_Instance.wrapper(int, int, Grasshopper.DataTree, System.Collections.Generic.List)': not all code paths return a value (line 87)

public List<int> wrapper(int br, int depth, DataTree<int> topo, List<double> vals)
{
    List<int> collection = new List<int>();
    collection.Add(br);
    if (depth > 0)
    {
        double[] area = new double[vals.Count - 1];
        for (int i = 0;i < topo.Branches[br].Count;i++) 
        {
            area[i] = vals[topo.Branch(br)[i]];
        }
        double Im = area.Max();

        int IofMaxArea = area.ToList().IndexOf(Im);
        collection.Add(IofMaxArea);
        //  wrapper(topo.Branch(br)[IofMaxArea], de, topo, vals);
        wrapper(topo.Branch(br)[IofMaxArea], depth-1, topo, vals);
    }
    if (depth == 0)
    {
        return collection;
    }
}

我正在尝试改进一个python脚本,它运行得很好,但速度太慢,无法满足我的需要。这是python代码。在

^{pr2}$

这是Rhino3d+蚱蜢。在


Tags: brbranchnewreturnareawrapperlistcollection
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:14

这里的代码确保depth的任何正值都会导致方法再次被调用:

if (depth > 0)
{
    ...
    ...
    wrapper(topo.Branch(br)[IofMaxArea], depth-1, topo, vals);
}

超越if块的唯一方法是depth小于或等于0。在此之后,不需要单独验证depth的值。在

所以改变这个:

^{pr2}$

为此:

return collection;

编译器正在抱怨,因为您没有指定如果depth不等于0,则返回什么值。即使在逻辑上你知道你的方法最终会返回一个值,编译器却不会

相关问题 更多 >