使用循环计算C#中的阶乘

3
这是我目前为止的内容:

这是我目前的进展:

namespace factorials
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;

            do
            {
                Console.WriteLine("What non-negative integer do you want to factorial?");
                while (!int.TryParse(Console.ReadLine(), out number))
                    Console.WriteLine("Please enter a whole number only");
                calculate(ref number);
            } while (number >= 0);
            Console.WriteLine("Please enter a non-negative number");

        }
        static void calculate(ref int number)
        {
            int factorial;
            int counter;

            for (counter = number; counter <= number; counter++)
            {
                factorial = number * number;
                Console.WriteLine("The factorial of {0} is {1}", number, factorial);
            }

    }
    }
    }

现在它只给出了数字的平方,而不是它们的阶乘。如何使程序重复输入次数,从而得出阶乘结果?

此外,我不确定是否有必要仅限于非负整数,但如果我想这样做,那么该部分只需在此处结束程序,而不是回到开始。

8个回答

7

for循环根本没有任何意义!如果您正在寻找阶乘,那么您必须将从1到给定数字的所有数字相乘。应该这样写:

int factorial = 1;

for (counter = 1; counter <= number; counter++)
{
    factorial = factorial * counter;

}

Console.WriteLine("The factorial of {0} is {1}", number, factorial);

这将计算一个数字的阶乘。如果你想要计算多个数字的阶乘,你需要重复这个过程。

1
你的循环将数字的平方赋值给结果,并立即退出。你需要改变它,使得结果被从1到N(包括)的数反复相乘。
将1分配给阶乘,并在循环中将其乘以计数器:
factorial *= counter;

不要忘记将计数器从1开始。

0
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            userinputF = Int32.Parse(txtFaculteit.Text);
            for (counter = 1; counter <= userinputF; counter++)
            {
                answer = answer *=  counter;
            }          
        }
        catch (Exception exception)
        {

                MessageBox.Show("Please fill in a number " + exception.Message);
        }

        lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
    }
}

}


0
    int userinputF;
    int counter;
    int answer =1;


    public Form1()

    {
        InitializeComponent();

    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            userinputF = Int32.Parse(txtFaculteit.Text);
            for (counter = 1; counter <= userinputF; counter++)
            {
                answer = answer *=  counter;
            }          
        }
        catch (Exception exception)
        {

                MessageBox.Show("Please fill in a number " + exception.Message);
        }

        lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
    }
}

}


请在您的帖子中提供更多的解释。 - rbr94

0
static void Main(string[] args)
    {

        Console.WriteLine("Enter an integer number to factorise");

        int ans = 1;
        int a = int.Parse(Console.ReadLine());

        for (int i = 1; i <= a; i++)
        {
            ans = ans * i;
        }
        Console.WriteLine(ans.ToString());
        Console.ReadLine();             
    }

0

递归实现以及基本实现如下所示

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;
        }
    }
  }
}

0
        string str = Interaction.InputBox("Enter the number to find the factorial of: ");
        double counter = double.Parse(str);
        long factorial = 1;

        while (counter > 1)
        {
            factorial *= (long)counter--;//factorial = factorial * counter - 1
        }
        MessageBox.Show("The factorial of " + str + " is " + String.Format("{0:N}", factorial));

我在使用Microsoft.VisualBasic参考库来调用Interaction.InputBox()方法。


0

我会给你一些提示。

  1. 初始化一个变量为1,我们称之为Answer
  2. 运行一个循环,从1到Number进行操作Answer = Answer * loopIterationVariable。每次迭代后,将循环迭代变量增加1。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接