如何从用户存储中获取数字,直到他们键入 -1,然后将所有数字加起来

How to get Numbers from user store them until they type -1 and than add all the numbers up toghterr

嘿,我正在尝试找出从用户那里获取数字的最佳方法,而不是一旦用户键入 -1,它将把所有数字相加

这个和你的问题描述的一样,你可以测试一下here

using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        List<int> numbers = new List<int>();
        Console.WriteLine("Enter your numbers below, 1 per line");
        string input = "";
        int inputNumber = 0;
        while((input = Console.ReadLine()) != "-1"){
            if(int.TryParse(input, out inputNumber)){
               numbers.Add(inputNumber);
            }
        }
        int sum = numbers.Sum();
        Console.WriteLine("Total Sum: " + sum);
    }
    
}