Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET Web应用程序中的C#递归:在列表框中显示静态方法中的数字_C#_Asp.net_Web Applications_Recursion - Fatal编程技术网

ASP.NET Web应用程序中的C#递归:在列表框中显示静态方法中的数字

ASP.NET Web应用程序中的C#递归:在列表框中显示静态方法中的数字,c#,asp.net,web-applications,recursion,C#,Asp.net,Web Applications,Recursion,我正在努力学习C#中的递归。我能够在控制台应用程序中进行简单的倒计时,但我不确定如何在ASP.NET web应用程序中进行同样的操作。我希望在列表框中显示结果,但无法在静态函数中访问列表框 以下是我的控制台应用程序的配置: static void Main(string[] args) { int startInt = 100; countDown(startInt); Console.ReadLine(); }

我正在努力学习C#中的递归。我能够在控制台应用程序中进行简单的倒计时,但我不确定如何在ASP.NET web应用程序中进行同样的操作。我希望在列表框中显示结果,但无法在静态函数中访问列表框

以下是我的控制台应用程序的配置:

    static void Main(string[] args)
    {
        int startInt = 100;
        countDown(startInt);
        Console.ReadLine();

    }
    public static void countDown(int integer)
    {
        if (integer == 1)
        {
            Console.WriteLine(integer);
        }
        else 
        {
            Console.WriteLine(integer);
            integer--;
            countDown(integer);
        }
    }   

在将在列表框中显示数字的web应用程序中实现此功能有什么帮助吗?

您不需要使其保持静态,因为您不再从静态函数调用它。使用像这样的倒计时函数

public void countDown(int integer)
{
    if (integer > 0)
    {
        ListBox1.Items.Add(integer.ToString());
        integer--;
        countDown(integer);
    }
} 
应该很好用。我试了一下

int startInt = 100;
countDown(startInt);

在Page_Load方法中,列表框按预期显示。

您不需要保持它为静态,因为您不再从静态函数调用它。使用像这样的倒计时函数

public void countDown(int integer)
{
    if (integer > 0)
    {
        ListBox1.Items.Add(integer.ToString());
        integer--;
        countDown(integer);
    }
} 
应该很好用。我试了一下

int startInt = 100;
countDown(startInt);

在页面加载方法中,列表框按预期显示。

我不确定是否存在性能问题或更好的解决方案。但这是可行的,可以作为不受限制的递归的一个很好的例子

protected void Page_Load(object sender, EventArgs e)
{
    int startInt = 100;
    form1.Controls.Add(countDown(startInt, new ListBox()));
}

public static ListBox countDown(int integer, ListBox lb)
{
    ListItem li = new ListItem();

    if (integer > 0)
    {
        li.Text = integer.ToString();
        lb.Items.Add(li);

        integer--;
        countDown(integer, lb);
    }
    return lb;

}   

我不确定是否存在性能问题或更好的解决方案。但这是可行的,可以作为不受限制的递归的一个很好的例子

protected void Page_Load(object sender, EventArgs e)
{
    int startInt = 100;
    form1.Controls.Add(countDown(startInt, new ListBox()));
}

public static ListBox countDown(int integer, ListBox lb)
{
    ListItem li = new ListItem();

    if (integer > 0)
    {
        li.Text = integer.ToString();
        lb.Items.Add(li);

        integer--;
        countDown(integer, lb);
    }
    return lb;

}   

将列表框作为附加参数传递。请看我对上一个问题的回答,该问题给出了在C#ASP.NET应用程序中使用递归的示例。请将列表框作为附加参数传递。请看我对上一个问题的回答,该问题给出了在C#ASP.NET应用程序中使用递归的示例。问题在于访问来自静态方法的对象。所以你的答案不合适,我猜。多么不幸的代码重复。问题是如何从静态方法访问对象。所以你的答案不合适,我猜。多么不幸的代码重复。它真的应该重写。