简体   繁体   English

C#While循环(添加用户输入以达到目标)

[英]C# While Loop (adding the user input to reach a target)

I need to create a program that adding the user input to reach a target, as a result, is just like below. 我需要创建一个程序,添加用户输入以达到目标,结果如下所示。

在此处输入图片说明

I have to use 'While Loop' for this, but it is difficult for me to use while loop... 我必须为此使用“ While循环”,但很难使用while循环...

Here is my code 这是我的代码

Console.WriteLine("Enter Target Value: 6");

int total = 0;
int target = 6;
int i;
for (i = 1; i <= 4; i++)
{
    Console.Write("Enter #{0}:\t", i);
    total += Convert.ToInt32(Console.ReadLine());
}

while (total == target);

Console.WriteLine("It took {0} inputs to take the sum to\t{1}",i, total);
Console.ReadLine();

Could you please help me to find the problems? 您能帮我发现问题吗?

Here is the complete example. 这是完整的示例。

  static void Main(string[] args)
    {


        try
           {
            int i = 0;
            int number;
            int input=0;
            Console.WriteLine("Enter target number ");

            number =   int.Parse(Console.ReadLine());
            while (input != number && input < number)
            {
                Console.WriteLine($"Enter number  {i+1}");
                input += int.Parse(Console.ReadLine());
                i++;


            }
            Console.WriteLine($"It took {i} number to make the sum {number}");


          }
          catch (Exception e)
          {



          }


        Console.ReadLine();
    }

Do you know what number the user will enter? 您知道用户将输入什么号码吗? No, you do not know. 不,你不知道。 So you do not know how many numbers will it take to reach the sum as well. 因此,您不知道要达到总和还需要多少个数字。

Pick the right tool for the job. 选择适合该工作的工具。

For Loop 对于循环

A "For" Loop is used to repeat a specific block of code a known number of times. “ For”循环用于将特定代码块重复已知次数。

While Loop While循环

A "While" Loop is used to repeat a specific block of code an unknown number of times, “ While”循环用于未知次数重复特定代码块,

Given the above 2 options, your pick should be a while loop since you do NOT know how many times you will need to ask the user to enter a number to reach the sum. 给定以上两个选项,您应该选择一个while循环,因为您不知道需要多少次才能要求用户输入数字来达到总和。 It may be 1 or many, many times. 可能是1次或多次。

In C#, there is also the do while loop, which is to be used if you know you must do something at least once and possibly more, Therefore, for your case the best option would be to use do while . 在C#中,还有一个do while循环,如果您知道必须至少执行一次甚至可能多次执行某事,则可以使用do while循环,因此,对于您而言,最好的选择是使用do while

You may read more on while loop , for loop , and do while . 您可以阅读更多有关while循环for循环while的内容

Your code is perfectly operational. 您的代码是完全可操作的。 I hope this helps: 我希望这有帮助:

Console.WriteLine("Enter Target Value: 6");

int total = 0;
int target = 6;
int i = 1;

while (i <= 4)
{
    Console.Write("Enter #{0}:\t", i);
    total += Convert.ToInt32(Console.ReadLine());
    i++;
}

while (total == target);
Console.WriteLine("It took {0} inputs to take the sum to\t{1}",i, total);

Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM