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

    }
    }
    }

现在它只给我数字的平方,而不是它们的阶乘。如何让它重复输入的次数,从而产生阶乘?

另外我不确定是否有必要将程序限制为仅非负整数,但如果我想要那部分只是在那里结束程序而不是循环回到开头。

4

8 回答 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);

这将计算一个数字的阶乘。您必须为所有想要的数字重复它。

于 2013-03-04T00:32:06.267 回答
1

您的循环将数字的平方分配给循环中的结果,然后立即退出。您需要对其进行更改,以使结果重复乘以从 1 到 N 的数字(包括 1 到 N)。

将 1 分配给阶乘,并在循环中乘以计数器:

factorial *= counter;

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

于 2013-03-04T00:30:35.520 回答
0

我会给你提示。

  1. 将变量初始化为 1。假设它为Answer.
  2. 运行从 1 到 Number 的循环,执行 的操作Answer = Answer * loopIterationVariable。每次迭代后,循环迭代变量加 1。
于 2013-03-04T00:31:18.777 回答
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();             
    }
于 2013-09-30T14:41:50.140 回答
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()

于 2013-11-01T23:14:59.520 回答
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;
        }
    }
  }
}
于 2014-11-16T04:31:35.470 回答
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;
    }
}

}

于 2016-10-20T08:24:58.103 回答
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;
    }
}

}

于 2016-10-20T08:35:21.613 回答