C#阶乘计算器工作不正常

C#阶乘计算器工作不正常,c#,math,factorial,C#,Math,Factorial,我的阶乘计算器工作不太正常 正如我的教授所希望的,它在1点到20点之间正常工作。但是,输入0应返回1的阶乘;它返回0 这是我的密码: private void CalculateFactorial(long number) { //perform the calculations long result = number; for (int i = 1; i < number; i++) {

我的阶乘计算器工作不太正常

正如我的教授所希望的,它在1点到20点之间正常工作。但是,输入0应返回1的阶乘;它返回0

这是我的密码:

        private void CalculateFactorial(long number)
    {
        //perform the calculations
        long result = number;
        for (int i = 1; i < number; i++)
        {
            result = result * i;
        }

        //display the calculated Factorial to the user
        txt_Factorial.Text = result.ToString("n0");
    }
private void CalculateFactorial(长数字)
{
//进行计算
长结果=数字;
对于(int i=1;i
下面是调用上述方法的方法,即calculate按钮的事件处理程序:

private void btn_Calculate_Click(object sender, EventArgs e)
    {
        //get the users input
        long number = long.Parse(txt_Number.Text);

        // make sure the number not invalid. If it is invalid, tell the user
        // otherwise, proceed to calculation. 
        if (number < 0 || number > 20)
            txt_Factorial.Text = "Invalid Number";
        else
            CalculateFactorial(number);
        txt_Number.Focus(); // returns the focus to the number box whether or not data was valid
private void btn\u计算\u单击(对象发送方,事件参数e)
{
//获取用户输入
long number=long.Parse(txt_number.Text);
//确保号码没有无效。如果无效,请告诉用户
//否则,继续计算。
如果(数字<0 | |数字>20)
txt_Factorial.Text=“无效数字”;
其他的
计算因子(数);
txt_Number.Focus();//返回数字框的焦点,无论数据是否有效

想法?

如果你在调试器中逐步解决这个问题,问题就会变得非常清楚。由于你刚刚开始编程,我强烈建议你尽早习惯调试器。它是编程的绝对无价工具

查看你的
循环:

for (int i = 1; i < number; i++)
您可以使用以下选项:

if(number == 0){
    result = 1;
}
for (int i = 1; i <= number; i++)
    result *=  i;
}
if(number==0){
结果=1;
}

对于(int i=1;i0的阶乘定义为1,而不是计算为1,并且您的代码没有反映这一点。请在代码之前添加一个检查:

if (number == 0)
    result = 1;
else 
    // compute factorial

还可以考虑创建一个返回整数值作为结果的函数。

您可以测试以下代码!已测试且有效。递归实现和基本实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
    class Program
    {
        static void Main(string[] args)
        {

        NumberManipulator manipulator = new NumberManipulator();
        Console.WriteLine("Please Enter Factorial Number:");
        int a= Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("---Basic Calling--");
        Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a));

        Console.WriteLine("--Recursively Calling--");
        Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a));

        Console.ReadLine();
    }
}

class NumberManipulator
{
    public int factorial(int num)
    {
        int result=1;
        int b = 1;
        do
        {
            result = result * b;
            Console.WriteLine(result);
            b++;
        } while (num >= b);
        return result;
    }

    public int recursively(int num)
    {
        if (num <= 1)
        {
            return 1;
        }
        else
        {
            return recursively(num - 1) * num;
        }
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序50
{
班级计划
{
静态void Main(字符串[]参数)
{
NumberManipulator操纵器=新的NumberManipulator();
Console.WriteLine(“请输入阶乘号:”);
int a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“--Basic Calling--”);
WriteLine({0}的阶乘是:{1}),a,操纵器.Factorial(a));
WriteLine(“--递归调用--”);
WriteLine({0}的阶乘为:{1}),a,操纵器。递归(a));
Console.ReadLine();
}
}
类编号计算器
{
公共整数阶乘(整数)
{
int结果=1;
int b=1;
做
{
结果=结果*b;
控制台写入线(结果);
b++;
}而(num>=b);
返回结果;
}
递归公共int(int num)
{

if(num)在顶部添加一个0的检查?这是像这样的数学函数的一个非常常见的操作。感谢你在其中加入了一些逻辑。我正在阅读递归的方法,但是自从我在Python中有一个类似的任务要做以来,我已经有一年多没有使用递归了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
    class Program
    {
        static void Main(string[] args)
        {

        NumberManipulator manipulator = new NumberManipulator();
        Console.WriteLine("Please Enter Factorial Number:");
        int a= Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("---Basic Calling--");
        Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a));

        Console.WriteLine("--Recursively Calling--");
        Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a));

        Console.ReadLine();
    }
}

class NumberManipulator
{
    public int factorial(int num)
    {
        int result=1;
        int b = 1;
        do
        {
            result = result * b;
            Console.WriteLine(result);
            b++;
        } while (num >= b);
        return result;
    }

    public int recursively(int num)
    {
        if (num <= 1)
        {
            return 1;
        }
        else
        {
            return recursively(num - 1) * num;
        }
    }
  }
}