【发布时间】:2020-08-14 15:30:24
【问题描述】:
我对 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;。
标签: c# while-loop do-while