【发布时间】:2013-11-28 18:05:45
【问题描述】:
我正在学习递归,我需要乘以两个给定数字的幂。当我运行我的代码时它不起作用(意味着控制台上没有显示任何内容)
方法:
static int multiply (int x, int y)
{
if ( y == 1 )
return x ;
else
return (x + multiply(x, y - 1));
}
static int power(int x,int y)
{
if (y == 0)
return 0;
else
return (x * power(x, y - 1));
}
主要方法:
static void Main(string[] args)
{
multiply(2, 4);
power(2, 5);
Console.ReadLine();
}
有人有什么想法吗?我有一种感觉,我在做一些明显很愚蠢的事情。
【问题讨论】:
-
你永远不会输出任何东西..
-
您为什么希望控制台上显示一些内容?
-
if (y == 0) return 0;这里应该是return 1 -
用于乘法 if ( y == 0 ) return 0 ;否则 if ( y == 1 ) 返回 x ;否则...
标签: c# recursion console.writeline