新手提问时间:递归 [英] Newbie Question time: RecursiveFactorial question
问题描述
大家好,一个问题,
我从Micorosoft教科书中键入了以下代码:
I typed in the following code from a Micorosoft text book:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间RecursiveFactorial
{
课程计划
{
静态void Main(string [] args)
{
阶乘(5);
}
public static int阶乘(int n)
{
如果(n == 0)
{
返回1; //基本情况
}
其他
{
返回n *阶乘(n-1); //递归案例
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RecursiveFactorial
{
class Program
{
static void Main(string[] args)
{
Factorial(5);
}
public static int Factorial(int n)
{
if (n == 0)
{
return 1; // base case
}
else
{
return n * Factorial(n - 1); //recursive case
}
}
}
}
唯一的问题是我得到了以下不需要的输出:
The only problem is that I get the following undesired output:
按任意键继续..."
"press any key to continue ..."
有人可以帮我吗
提前欢呼
Den-i又名纽伯
推荐答案
您好,我认为您缺少以下行:
hi there, I think you are missing the following line:
您有:
静态void Main(string [] args)
{
阶乘(5);
}
static void Main(string[] args)
{
Factorial(5);
}
应该
static void Main(string[] args) { Console.WriteLine(Factorial(5).ToString()); Console.ReadLine(); }
希望这会有所帮助:)
这篇关于新手提问时间:递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!