我在一所社区学院上计算机编程课的介绍,老师想让我们稍微看一看数字7的阶乘。
using System;
namespace TheLoop
{
class TheLoop
{
static void Main(string[] args)
{
int number = 7;
long factorial = number;
while (number > 1)
factorial *= --number;
System.Console.WriteLine(factorial);
Console.ReadKey();
}
}
}密码很好用。但是老师想让我们显示循环的每一步,例如
1. 7 * 6 = 42
2. 42 * 5 = 210
3. 210 * 4 = 840
4. 840 * 3 = 2520
5. 2520 * 2 = 5040
and so on, other than just displaying the result对不起,我刚到C#,有人能教我怎么做吗?
发布于 2014-10-06 23:55:31
那这个呢?
int number = 7;
int i = 1;
long factorial = number;
while (number > 1)
{
factorial *= --number;
Console.WriteLine("{0}. {1} * {2} = {3}", i++, factorial/number, number, factorial);
}
Console.WriteLine(factorial);发布于 2014-10-06 23:50:06
在您的while中,您可以访问当前数字和当前阶乘,如下所示:
while (number > 1)
{
// Print a Console.WriteLine() in here with what you want
factorial *= --number;
}发布于 2014-10-06 23:49:05
System.Console.WriteLine(数+“*”+--数+“=”+阶乘);
这行得通吗?
https://stackoverflow.com/questions/26226525
复制相似问题