【发布时间】:2014-07-22 19:41:16
【问题描述】:
我正在尝试让我的阶乘程序工作,它从用户那里获取一个数字并计算它的偶阶乘。即6!是 720,它的偶阶乘是 6x4x2 = 48,除了我已经弄清楚如何做阶乘部分但是每当我尝试添加更多代码以便我可以尝试计算其余部分时,我得到“运算符 % 不能应用于键入method group 和int”或“在类、结构或接口成员声明中出现意外符号{”,我似乎看不出我做错了什么。任何建议都会有所帮助
using System;
namespace Factorial
{
class Factorial
{
public static void Main()
{
Console.WriteLine("Enter a number");
int i =int.Parse(Console.ReadLine());
long fact = GetFactnum(i);
Console.WriteLine("{0} factorial is {1}", i, fact);
Console.ReadKey();
}
public static long GetFactnum(int i)
{
if (i == 0)
{
return 1;
}
return i * GetFactnum(i-1);
}
// public static void EvenFact()
// {
// int sumofnums =0;
// if((GetFactnum % 2) ==0)
// sumofnums += GetFactnum;
// }
}
}
【问题讨论】:
-
花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式设置与其他站点不同。您的帖子看起来越好,用户就越容易为您提供帮助。
-
1-
EvenFact应该采用输入参数。 2-if((GetFactnum % 2) ==0)是错误的。查看您之前的函数如何完成方法调用 3-GetFactnum是您尝试做的一个很好的例子。 4- 阅读一些 c# 文档
标签: c# declaration factorial