帮我看下程序错误出现在哪里

来源:百度知道 编辑:UC知道 时间:2024/06/22 22:55:11
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
public int Factorial(int n)
{
if(n<0)
return;
else if(n == 1 || n == 0)
return 1;
else
return n*Factorial(n - 1);
}
}
}

问:这个程序有错误?它总是显示我缺少个大括号。但是我添加后,它还是显示缺少大括号。谁能帮我改改,让我的程序调试成功。

这不是多一个括号少一个括号的问题
public int Factorial(int n)
{
if(n<0)
return;
else if(n == 1 || n == 0)
return 1;
else
return n*Factorial(n - 1);
}
同志啊,这段代码怎么能放到main函数里呢。。。。你应该放到main函数的上面面,另外if(n<0)
return;也是不对的,因为你返回的是整形,只有在声明为void的函数里才能用return
应该这样写
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static int Factorial(int n)
{
if (n < 0)
return n;
else if (n == 1 || n == 0)
return 1;
else
return n * Factorial(n - 1);
}

static void Main(string[] args)
{

}

}
}

在最后面添加一个}就行了。实在不行就把错误信息拿过来我帮你看看。

在最后在添加一个大括号,还少了一个。你自己数数,左边的大括号有四个,右边的大括号只有三个。

nzpcau 正解