1 回答
TA贡献1853条经验 获得超6个赞
问题在于
nFactorial.Text = factorial.ToString();
您正在覆盖每个循环迭代的 nFactorial。
你想要的(我假设)是附加文本,所以你想做这样的事情(格式化你想要的格式):
nFactorial.Text += factorial.ToString() + Environment.NewLine; // Environment.NewLine to display each value on a separate line, again format how you'd like
请注意,正如 Selman Genç 所提到的,您需要使用long而不是int(更改Int32为 long):
long facOut, factorial, number;
long num = 20;
把它们放在一起:
private void nFactorial_Click(object sender, EventArgs e)
{
long facOut, factorial, number;
long num = 20;
factorial = num;
for (facOut = num - 1; facOut >= 1; facOut--)
{
factorial *= facOut;
nFactorial.Text += factorial.ToString() + Environment.NewLine;
}
}
编辑:好的,你正在向后循环。你需要向上而不是向下工作。试试这个:
private void nFactorial_Click(object sender, EventArgs e)
{
long facOut, factorial;
long num = 20;
factorial = 1;
for (facOut = 1; facOut <= num; facOut++)
{
factorial *= facOut;
tb.Text += factorial.ToString() + Environment.NewLine;
}
}
注意:如果要跳过1,请facOut = 2在for循环中设置。
- 1 回答
- 0 关注
- 213 浏览
添加回答
举报
