-1

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

错误(CS0161):'Script_Instance.wrapper(int,int,Grasshopper.DataTree,Sy​​stem.Collections.Generic.List)':并非所有代码路径都返回值(第 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代码。

import Grasshopper.DataTree as ghdt
import ghpythonlib.components as ghcomp
import rhinoscriptsyntax as rs

newTree = ghdt[object]()

def wrapper(br,depth):
    if depth>0:
        area = []
        for k in range(topo.Branch(br).Count):
            ar = vals[topo.Branch(br)[k]]

            #polygonC = rs.CloseCurve(polygon)
            area.append(ar)

        IofMaxArea = area.index(max(area))
        collection.append([pts[topo.Branch(br)[IofMaxArea]],topo.Branch(br)[IofMaxArea]])
        #[topo.Branch(br)[IofMaxArea]
        wrapper(topo.Branch(br)[IofMaxArea],depth-1)
    else: return None

def isovister(b,d):

    global collection
    collection = []

    collection.append([pts[b],b])
    wrapper(b,d)
    return collection
if topo.Branch(item).Count !=0:
    results = isovister(item,de)
    a = tuple(x[0] for x in results)
    b = tuple(x[1] for x in results)
else:
    a = None
    b = None

这是在 Rhino3d+Grasshopper 中。

4

1 回答 1

1

depth此处的代码确保再次调用方法中的任何正值结果:

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

if超越该块的唯一方法是 ifdepth小于或等于 0。depth此后无需单独验证 的值。

所以改变这个:

if (depth == 0)
{
    return collection;
}

对此:

return collection;

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

于 2015-05-24T12:42:12.713 回答