【发布时间】:2016-06-30 03:44:20
【问题描述】:
当我输入数字 6 计算其阶乘时,它返回 30(这是错误的)。
为什么我的程序会产生不正确的输出?
using System;
namespace Scenario1_2
{
class Program
{
static void Main(string[] args)
{
int counter, number, fact;
Console.WriteLine("Please enter the number you wish to factorize");
number = int.Parse(Console.ReadLine());
fact = number;
for (counter = number - 1; counter >= 1; counter--)
{
fact = fact * counter;
Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);
Console.ReadLine();
}
}
}
}
【问题讨论】:
-
将你的 console.writeline 和 readline 移到循环之外。或者继续按回车,直到你得到答案。
-
哦,我明白了!非常感谢,这很有道理!
-
试试this