如何创建一个 C# 程序,该程序要求使用循环的值介于 1 和 100 之间

因此,我正在尝试创建一个 C# 程序,该程序要求一个介于 1 和 100 之间的值,该程序使用循环来确定 1 和输入值之间所有值的总和,如果输入的数字小于 1 或大于 100,它会打印出“对不起。再试一次。我已经为此工作了好几天,但我无法让它打印总和,我一直得到 0,每当我测试它并输入低于 1 或超过 100 的数字时,它都不会打印我想要的错误消息。代码如下:


using System;


namespace PrintSumL

{

class Program

{

    static void Main(string[] args)

    {


        Console.WriteLine("Enter a beginning value between 1 and 100");

        int s = Convert.ToInt32(Console.ReadLine());



        int sum = 0;


        Console.WriteLine("Sum of values: " + sum);

        Console.ReadKey();



        Random rand = new Random();

        rand.Next(1, 51);



        while (0 < s && s < 101)

        {

            sum += s;

            s++;

            if (s < 0 && 101 < s)

            {


                Console.WriteLine("Invalid input. Try again.");

            }


            {


            }

            {


            }

        }

    }

}

}


炎炎设计
浏览 65回答 4
4回答

饮歌长啸

您可以将程序视为从上到下逐行执行,并且仅在到达 while 循环结束时向上移动。while 循环的末尾是与循环开始时匹配的。}{知道了这一点,你就会明白为什么它总是说总和为零。从您的代码:int sum = 0;Console.WriteLine("Sum of values: " + sum);由于程序执行“从上到下逐行”,因此它将首先设置为 0,然后打印出总和。所以它会一直打印.如果您希望它在计算完总和后打印出总和,则需要将总和的下移到计算总和的位置下方。sum"Sum of values: 0"WriteLine同样的问题也适用于 : 打印此语句的行出现在 之后,因此只有在 0 到 101 之间时才会执行。由于您尝试捕获不在 0 和 101 之间的场景,因此您需要将语句移动到循环上方。"Invalid input. Try again."while (0 < s && s < 101)sswhile因此,要解决眼前的问题,只需做两件事:1)移动两条线Console.WriteLine("Sum of values: " + sum);Console.ReadKey();到 while 循环之后(紧跟在 which 之后,与 的缩进级别相同)。}while2)移动三条线if (s < 0 && 101 < s){&nbsp; &nbsp; Console.WriteLine("Invalid input. Try again.");}直到正下方,然后仔细检查逻辑。如果小于零或大于 101,则听起来要打印语句。int s = Convert.ToInt32(Console.ReadLine());ss

尚方宝剑之说

如果需要循环,则应遵循 Heath Raftery 说明否则你可以写这样的东西:&nbsp;static void Main(string[] args)&nbsp;{&nbsp; &nbsp; &nbsp;Console.WriteLine("Enter a beginning value between 1 and 100");&nbsp; &nbsp; &nbsp;int s = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp; &nbsp;if (s < 0 || s > 100)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine("Invalid input. Try again.");&nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine($"Sum of values: {Enumerable.Range(1,s).Sum()}");&nbsp;}或者正如 Haldo 所说,您可以使用 的公式并将最后一个 WriteLine 替换为:N * (N+1) / 2Console.WriteLine($"Sum of values: {s * (s+1) / 2}")

慕沐林林

这是一个有效的算法......&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Enter a value between 1 and 100");&nbsp; &nbsp; &nbsp; &nbsp; var input = int.Parse(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; if (input<1 || input>100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Sorry, Try again");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(input > 2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input-=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum+=input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Sum of values: " + sum);

婷婷同学_

试试这个:static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter a number between 1 and 100: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int Number = Convert.ToInt32(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Number < 0 || Number > 100)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Sorry. Try again.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= Number; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Sum of values: " + sum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP