帮助递归和返回值

发布于 2024-10-31 06:29:08 字数 843 浏览 1 评论 0原文

这是之前发布的问题的扩展。我正在尝试递归地构建一个字符串。我需要更改下面的函数来执行此操作 - 函数的每次递归都会生成所需的字符串,但我需要将它们连接在一起并返回整个字符串。 “相关”作为空字符串传递到函数中,我认为我使用 string.Format 的方式会将每个递归附加到“相关”字符串吗?显然不是。

不知道如何...

private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
    foreach (TaxonomyItemData item in taxData.TaxonomyItems)
        {
            if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
            {
                related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
            }                   
        }
    // Show all its sub categories
    foreach (TaxonomyData cat in taxData.Taxonomy)
        {   
            getRelatedNews(cat, related, contentTitle);
        }

    return(related);

}

This is an extension of a previously posted question. I'm trying to recursively build a string. I need to change the function below to do this - each recursion of the function generates the desired string, but I need to concat these together and return the whole string. 'related' is passed into the function as an empty string and I thought the way I was using string.Format would append each recursion to the 'related' string? Apparently not.

Not sure how...

private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
    foreach (TaxonomyItemData item in taxData.TaxonomyItems)
        {
            if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
            {
                related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
            }                   
        }
    // Show all its sub categories
    foreach (TaxonomyData cat in taxData.Taxonomy)
        {   
            getRelatedNews(cat, related, contentTitle);
        }

    return(related);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜血缘 2024-11-07 06:29:08
foreach (TaxonomyData cat in taxData.Taxonomy)
    {   
        getRelatedNews(cat, related, contentTitle);
    }

应该是

foreach (TaxonomyData cat in taxData.Taxonomy)
    {   
        related = getRelatedNews(cat, related, contentTitle);
    }

因为字符串是不可变的。

foreach (TaxonomyData cat in taxData.Taxonomy)
    {   
        getRelatedNews(cat, related, contentTitle);
    }

should be

foreach (TaxonomyData cat in taxData.Taxonomy)
    {   
        related = getRelatedNews(cat, related, contentTitle);
    }

because strings are immutable.

缪败 2024-11-07 06:29:08

好吧,试试这个......

related = getRelatedNews(cat, related, contentTitle);

我不确定你的逻辑和程序流程......然后我也认为递归函数必须这样调用......

private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
    foreach (TaxonomyItemData item in taxData.TaxonomyItems)
        {
            if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
            {
                related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
            }                   
        }
    // Show all its sub categories
    foreach (TaxonomyData cat in taxData.Taxonomy)
        {   
            related = getRelatedNews(cat, related, contentTitle);
        }

    return(related);

}

Well try this...

related = getRelatedNews(cat, related, contentTitle);

I am not sure about your logic and flow of the program... then also I think recursive function must be called like this....

private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
    foreach (TaxonomyItemData item in taxData.TaxonomyItems)
        {
            if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
            {
                related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
            }                   
        }
    // Show all its sub categories
    foreach (TaxonomyData cat in taxData.Taxonomy)
        {   
            related = getRelatedNews(cat, related, contentTitle);
        }

    return(related);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文