我对C#相当陌生。我的编程老师给我带来了以下问题:我必须编写一个程序,让您输入特定范围/间隔内的数字(他给我的范围是10到10,000,但您可以考虑自己的数字)。
我必须以“ while”或“ do-while”循环来完成。这是到目前为止我到达的地方:
int n, Sum = 0;
while ((Sum < 10000))
{
Console.Write("Type in a number from the interval [10; 9999]: ");
n = int.Parse(Console.ReadLine());
if ((n <= 10) || (n >= 9999))
{
Console.WriteLine("That number isn't in the range!");
Console.Write("Type in a number from the interval [10; 9999]");
n = int.Parse(Console.ReadLine());
我已经完成一半,但是当总和达到5位数时,我不知道如何使程序求和。预先感谢!
n
添加到Sum
。因此,不是使用第二个n = int.Parse(Console.ReadLine());
(实际上并没有做任何有用的事情,它只是重复您之前执行的检查),而是使用Sum += n;
。
+=
是Sum = Sum + n
的简短表达方式。