Issue
Hey i am trying to figure out the best way to get numbers from a user and than once the user types -1 it will add up all the numbers
Solution
This does as your question's described, you can test it 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);
}
}
Answered By - Dean Van Greunen Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.