C# 如何添加用户输入并显示输入的sentinel循环?

C# 如何添加用户输入并显示输入的sentinel循环?,c#,C#,当我运行它时,它会立即添加数字。我需要它,这样它就不会了。当用户输入-1时,我也不能让它关闭。任何帮助都将不胜感激。给您: class Program { static void Main(string[] args) { int sum = 0; int count = 0; Console.WriteLine("Please enter all the numbers you

当我运行它时,它会立即添加数字。我需要它,这样它就不会了。当用户输入-1时,我也不能让它关闭。任何帮助都将不胜感激。

给您:

  class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            int count = 0;

            Console.WriteLine("Please enter all the numbers you would like to add. When finished, enter -1");

            string numbers = Console.ReadLine();

            while (numbers != "-1")
            {
                sum += int.Parse(numbers);
                count++;
                Console.WriteLine(sum);
                numbers = Console.ReadLine();

            }

        }
    }

我不明白你的问题和你的问题。我需要它,这样当用户输入-1时,我也不能关闭它。我实际上投了你一票,谢谢你的帮助。非常感谢。谢谢@Itsallgravy,有用吗?(如果答案有,你能标记为答案吗?)是的,有,我会的
public class Program
{
    public static void Main(string[] args)
    {
        int sum = 0;
        int count = 0;
        int num = 0;

        Console.WriteLine("Please enter all the numbers you would like to add. When finished, enter -1");

        do 
        {
            string buffer = Console.ReadLine();

            if (Int32.TryParse(buffer, out num) && num != -1)
            {
                sum += num;
                count++;
                Console.WriteLine(String.Format("Entered: [{0}], Running total over [{1}] numbers is: [{2}].", num, count, sum));
            }
        }
        while (num != -1);
    }
}