Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsakamoto/8a84bac19be1e426889ae326e0dd304d to your computer and use it in GitHub Desktop.
Save jsakamoto/8a84bac19be1e426889ae326e0dd304d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine($"Enter digit numbers ({int.MinValue}~{int.MaxValue}).");
Console.WriteLine($"I'll totaling those numbers until enter 0.");
var sum = EnumEnteredNumbers()
.TakeWhile(n => n != 0)
.Sum();
Console.WriteLine($"Totaled number is: {sum}");
}
static IEnumerable<int> EnumEnteredNumbers()
{
for (;;)
{
Console.Write("> ");
var line = Console.ReadLine();
var num = default(int);
if (int.TryParse(line, out num))
yield return num;
else
Console.WriteLine($"ERROR: Enter digit number ({int.MinValue}~{int.MaxValue})");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment